i have real time database and getting data through firebaserecyclerAdapter from realtime database and want to show in rcylerview . by using database reference code is accessing firebase database but fields are not showing in recyclerview. However the data is not showing only in my application.
public class find_friends extends AppCompatActivity {
RecyclerView find_friends_recyclerList;
DatabaseReference databaseReference;
FirebaseRecyclerAdapter<Contacts,findFriendsViewHolder> adapter;
FirebaseRecyclerOptions<Contacts> options;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_friends);
find_friends_recyclerList=findViewById(R.id.find_friends_recyclerList);
find_friends_recyclerList.setHasFixedSize(true);
databaseReference= FirebaseDatabase.getInstance().getReference().child("Users");
options= new FirebaseRecyclerOptions.Builder<Contacts>()
.setQuery(databaseReference, Contacts.class)
.build();
adapter=new FirebaseRecyclerAdapter<Contacts, findFriendsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull findFriendsViewHolder holder, int position, #NonNull Contacts model) {
holder.name.setText(model.getName());
holder.status.setText(model.getStatus());
Picasso.get().load(model.getImages()).into(holder.profileImage);
}
#NonNull
#Override
public findFriendsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.friendlist_item,parent,false);
return new findFriendsViewHolder(view);
}
};
find_friends_recyclerList.setAdapter(adapter);
adapter.startListening();
}
public static class findFriendsViewHolder extends RecyclerView.ViewHolder{
TextView name,status;
CircleImageView profileImage;
public findFriendsViewHolder(View itemView) {
super(itemView);
name=itemView.findViewById(R.id.name);
status=itemView.findViewById(R.id.phone);
profileImage=itemView.findViewById(R.id.friend_image);
}
}
}
You must provide a layout manager for the recycler view. For instance, a LinearLayoutManager.
Add this line of code:
find_friends_recyclerList.setLayoutManager(new LinearLayoutManager(this))
Related
I am trying to retrieve some data from Firebase Firestore although I am able to show the data on Logcat window but when I add it into my recycler view it doesn't show , recycler view doesn't inflate. I am unable to figure it out what I am currently missing although code runs without any error.
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference sellerRef;
RecyclerView mRecyclerView;
List<Products> mProducts;
Adapter adapter;
String TAG="MyTag";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.mRecyclerView);
mProducts=new ArrayList<>();
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(gridLayoutManager);
adapter = new Adapter(this,mProducts);
mRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
sellerRef=db.collection("Sellers");
Query query =sellerRef.whereEqualTo("City_Name","Sydney");
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
#Override
public void onComplete(#NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (QueryDocumentSnapshot document : task.getResult()) {
Products products =document.toObject(Products.class);
mProducts.add(products);
Log.d(TAG,""+products.getCity_Name()+" "+mProducts.size());
}
}else {
Toast.makeText(MainActivity.this, "Failed to load Data", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Adapter
private Context mContext;
private List<Products> mProducts;
String TAG = "AdTag";
public Adapter(Context mContext, List<Products> mProducts) {
this.mContext = mContext;
this.mProducts= mProducts;
}
#NonNull
#Override
public Adapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mContext).inflate(R.layout.grid_view_layout,parent,false);
return new Adapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull Adapter.ViewHolder holder, int position) {
Products products=mProducts.get(position);
holder.title.setText(products.getCity_Name());
if (products.getThumbnailUrl().equals("default")){
holder.gridIcon.setImageResource(R.mipmap.ic_launcher);
} else {
Glide.with(mContext).load(products.getThumbnailUrl()).into(holder.gridIcon);
}
}
#Override
public int getItemCount() {
return mProducts.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView title;
public ImageView gridIcon;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.textView2);
gridIcon=itemView.findViewById(R.id.imageView2);
}
}
}
Call adapter.notifyDataSetChanged(); after update data
I'm not sure what am doing wrong but I have a recyclerview populated with firebase firestore using FirestoreRecyclerAdapter. My challenge is that I can add documents and retrieve them to the recyclerview but for some reason, the adapter doesn't set Text.
This is my Adapter
public class CategoriesAdapter extends FirestoreRecyclerAdapter<BookCategory, CategoriesAdapter.CategoryHolder> {
public CategoriesAdapter(#NonNull FirestoreRecyclerOptions<BookCategory> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull CategoryHolder categoryHolder, int i, #NonNull BookCategory bookCategory) {
categoryHolder.txtCategory.setText(bookCategory.getCategory());
categoryHolder.txtDescription.setText(bookCategory.getDescription());
}
#NonNull
#Override
public CategoryHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item,
parent, false);
return new CategoryHolder(v);
}
class CategoryHolder extends RecyclerView.ViewHolder{
TextView txtCategory, txtDescription;
public CategoryHolder(#NonNull View itemView) {
super(itemView);
txtCategory = itemView.findViewById(R.id.Category);
txtDescription = itemView.findViewById(R.id.Description);
}
}
I managed to find the issue. The problem was in the class, I had changed the private variables
As mentioned in the title, my recyclerview does not work. No idea why, because I do not get any errors or anything in the logcat or while building the app. Is there a way I could debug this or could you point out the mistake that I did?
I've implemented another recyclerview with the adapter, but I used a simple adapter. I tried using the firebaseUI in this case, and it seems that the adapter somehow cannot retrieve the data from the firebase firestore.
https://imgur.com/a/JSLXuou The screenshot of my database structure.
My adapter:
public class ManageAdapter extends FirestoreRecyclerAdapter<Manage, ManageAdapter.ManageHolder> {
public ManageAdapter(#NonNull FirestoreRecyclerOptions<Manage> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ManageHolder holder, int position, #NonNull Manage model) {
holder.textviewName.setText(model.getName());
holder.textviewGrade.setText(model.getGrade());
//holder.textviewAge.setText(String.valueOf(model.getAge()));
}
#NonNull
#Override
public ManageHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.manage_item,
parent,false);
return new ManageHolder(v);
}
class ManageHolder extends RecyclerView.ViewHolder{
TextView textviewName;
TextView textviewAge;
TextView textviewGrade;
public ManageHolder(#NonNull View itemView) {
super(itemView);
textviewAge = itemView.findViewById(R.id.manage_item_title_age);
textviewGrade = itemView.findViewById(R.id.manage_item_grade);
textviewName = itemView.findViewById(R.id.manage_item_name);
}
}
}
Main class:
private FirebaseFirestore firebaseFirestore;
private Toolbar mToolbar;
private Button delete_event;
private RecyclerView participants;
private TextView manage_events_title;
private TextView participans_text;
private ManageAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_events);
final String ID = getIntent().getExtras().getString("ID");
firebaseFirestore = FirebaseFirestore.getInstance();
delete_event = (Button) findViewById(R.id.delete_event);
manage_events_title = (TextView) findViewById(R.id.manage_events_title);
participans_text = (TextView) findViewById(R.id.participants_text);
participants = (RecyclerView) findViewById(R.id.recyclerview_manage);
setUpRecyclerView();
}
private void setUpRecyclerView(){
final String ID = getIntent().getExtras().getString("ID");
Query query = firebaseFirestore.collection("Events").document(ID).collection("Users");
FirestoreRecyclerOptions<Manage> options = new FirestoreRecyclerOptions.Builder<Manage>()
.setQuery(query, Manage.class).build();
adapter = new ManageAdapter(options);
participants.setHasFixedSize(true);
participants.setLayoutManager(new LinearLayoutManager(this));
participants.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
This question already has answers here:
How to implement Firebase Recycler Adapter in newer version of Android 3.1 and higher?
(2 answers)
Closed 3 years ago.
So, I watched some tutorial on how to do the RecyclerView mainly this. I tweaked the code to fit my needs. But when i try to ran it, the RecyclerView isn't displaying on the fragment. My guess is the problem lies in adapter and viewholder part, because i'm not sure how to implement it yet. Can anyone help me?
The Fragment.java
public class JobListFragment extends Fragment {
RecyclerView mRecyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
public JobListFragment(){
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
((MainHomeActivity)getActivity()).setActionBarTitle("Job List");
View view = inflater.inflate(R.layout.fragment_job_list, container, false);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference().child("Data");
mRecyclerView = view.findViewById(R.id.recycleView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setHasFixedSize(true);
return view;
}
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerOptions<Model> options =
new FirebaseRecyclerOptions.Builder<Model>()
.setQuery(databaseReference, Model.class).build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.row, viewGroup, false);
return new ViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull Model model) {
holder.setDetails(model.getTitlept(), model.getDescpt(), model.getImagept());
}
};
mRecyclerView.setAdapter(adapter);
}
}
ViewHolder.java
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public ViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setDetails(String title, String description, String image){
TextView mTitleTv = mView.findViewById(R.id.rTitleTv);
TextView mDescTv = mView.findViewById(R.id.rDescIv);
ImageView mImageIv = mView.findViewById(R.id.rImageIv);
mTitleTv.setText(title);
mDescTv.setText(description);
Picasso.get().load(image).into(mImageIv);
}
}
Model.java
public class Model {
String titlept, imagept, descpt;
public Model(){}
public String getTitlept() {
return titlept;
}
public void setTitlept(String titlept) {
this.titlept = titlept;
}
public String getImagept() {
return imagept;
}
public void setImagept(String imagept) {
this.imagept = imagept;
}
public String getDescpt() {
return descpt;
}
public void setDescpt(String descpt) {
this.descpt = descpt;
}
}
Can you write the problem a little bit more clearly? Picture not displayed? #AdhityoPutro
This question already has answers here:
FirebaseRecyclerAdapter unable to populate result [duplicate]
(1 answer)
FirebaseListAdapter not pushing individual items for chat app - Firebase-Ui 3.1
(2 answers)
Closed 5 years ago.
I am following a youtube video in which I was trying to retrieve data using FirebaseUI but I am not able to show any data in the recyclerview.
Here is the code
public class MemeFragment extends Fragment {
RecyclerView recyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference myref;
FirebaseRecyclerAdapter<Datafileinfo, ShowDataViewHolder> firebaseRecyclerAdapter;
FirebaseRecyclerOptions<Datafileinfo> options;
public MemeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_meme, container, false);
firebaseDatabase = FirebaseDatabase.getInstance();
myref = FirebaseDatabase.getInstance().getReference("memes");
recyclerView = (RecyclerView) view.findViewById(R.id.recyc);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
options = new FirebaseRecyclerOptions.Builder<Datafileinfo>()
.setQuery(myref, Datafileinfo.class)
.build();
return view;
}
#Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Datafileinfo, ShowDataViewHolder>
(options) {
#Override
protected void onBindViewHolder(#NonNull ShowDataViewHolder holder, int position, #NonNull Datafileinfo model) {
holder.ImageTitle(model.getTitle());
holder.ImageUrl(model.getImage());
}
#Override
public ShowDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custo, parent, false);
return new ShowDataViewHolder(view);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class ShowDataViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ShowDataViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
textView = (TextView) itemView.findViewById(R.id.textView);
}
private void ImageTitle(String title) {
textView.setText(title);
}
private void ImageUrl(String url) {
Glide.with(itemView.getContext()).
load(url).
into(imageView);
}
}
}
and datafileinfo is the getter and setter class I have also taken the internet permission.Note I am able to get the single value from firebase but for multiple values, I am facing a problem this is the database.
where I am doing wrong any idea??
Note I have followed and gone through all the tutorials and similar post.
Create a separate Class for your ViewHolder
public class ShowDataViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
private TextView textView;
public ShowDataViewHolder (View itemView) {
super(itemView);
imageView = (ImageView)itemView.findViewById(R.id.imageView);
textView = (TextView) itemView.findViewById(R.id.textView);
}
}
In your MenuFragment add
DatabaseReference mDatabaseMemes= //your reference to memes node
FirebaseRecyclerAdapter<yourModel, ShowDataViewHolder> adapter= new FirebaseRecyclerAdapter<yourModel, ShowDataViewHolder>(yourModel.class, R.layout.yourLayout,
ShowDataViewHolder.class,mDatabaseMemes) {
#Override
protected void populateViewHolder(ShowDataViewHolder viewHolder, yourModel model, int position) {
viewHolder.textView.setText(model.getTitle());
Glide.with(context)
.load(model.getImage())
.fitCenter()
.into(viewHolder.imageView);
}
};
recyclerView.setAdapter(adapter);
}
MAKE SURE your model class has String title and image same as your child names under memes node.