+1 vote
72 views
by

What I basically wanna do is look inside the user_id for the name and retrieve it inside my TextView

1 Answer

0 votes
by
 
Best answer

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:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view"/>

And a .XML file for your item file, which looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/image_thumb_text_view" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/user_id_text_view" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/image_url_text_view" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/desc_text_view" />
</LinearLayout>

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<BlogPost> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<BlogPost>()
        .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<BlogPost, BlogPostHolder> firebaseRecyclerAdapter;

And instantiate it in your activity like this:

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<BlogPost, BlogPostHolder>(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();
    }
}
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...