My structure
So I have an app in which users upload posts in my adapter. I can retrieve the post description and the post picture, but when I try to retriev
In order to make it work correctly, I recommend you to do some changes in you model class as well as in your code. Your model class should look like:
public class BlogPost {
public String imageThumb, userId, imageUrl, desc;
public BlogPost() {}
public BlogPost(String imageThumb, String userId, String imageUrl, String desc) {
this.imageThumb = imageThumb;
this.userId = userId;
this.imageUrl = imageUrl;
this.desc = desc;
}
public String getImageThumb() {return imageThumb;}
public String getUserId() {return userId;}
public String getImageUrl() {return imageUrl;}
public String getDesc() {return desc;}
}
Please see the naming convention of the fields and getters.
In order to make it work, don't forget the remove the old data and add fresh one.
Assuming your have a .XML file for your activity that contains a RecyclerView
which looks like this:
And a .XML file for your item file, which looks like this:
To display your data in a RecyclerView
using a FriebaseRecyclerAdapter
, please follow the next steps:
First, you need to find the RecyclerView
in your activity and set the LinearLayoutManager
like this:
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Then you need to create the root reference of your Firebase database and a Query
object like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Users");
Then you'll have to create a FirebaseRecyclerOptions
object like this:
FirebaseRecyclerOptions firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder()
.setQuery(query, BlogPost.class)
.build();
In your activity class, create a holder
class that looks like this:
private class BlogPostHolder extends RecyclerView.ViewHolder {
private TextView imageThumbtextView, userIdTextView, imageUrlTextView, descTextView;
BlogPostHolder(View itemView) {
super(itemView);
imageThumbtextView = itemView.findViewById(R.id.image_thumb_text_view);
userIdTextView = itemView.findViewById(R.id.user_id_text_view);
imageUrlTextView = itemView.findViewById(R.id.image_url_text_view);
descTextView = itemView.findViewById(R.id.desc_text_view);
}
void setBlogPost(BlogPost blogPost) {
String imageThumb = blogPost.getImageThumb();
imageThumbtextView.setText(imageThumb);
String userId = blogPost.getUserId();
userIdTextView.setText(userId);
String imageUrl = blogPost.getImageUrl();
imageUrlTextView.setText(imageUrl);
String desc = blogPost.getDesc();
descTextView.setText(desc);
}
}
Then create an adapter which is declared as global:
private FirebaseRecyclerAdapter firebaseRecyclerAdapter;
And instantiate it in your activity like this:
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter(firebaseRecyclerOptions) {
@Override
protected void onBindViewHolder(@NonNull BlogPostHolder blogPostHolder, int position, @NonNull BlogPost blogPost) {
blogPostHolder.setBlogPost(blogPost);
}
@Override
public BlogPostHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new BlogPostHolder(view);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
In the end, don't forget to override the following two methods and start listening for changes:
@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
if (firebaseRecyclerAdapter!= null) {
firebaseRecyclerAdapter.stopListening();
}
}