如何从Firebase检索数据到适配器

R.Y*_*sef 6 android firebase firebase-realtime-database

我的结构

在此处输入图片说明

因此,我有一个应用程序,用户可以在其中上载适配器中的帖子。我可以检索帖子说明和帖子图片,但是当我尝试检索发布者的名称时,应用似乎崩溃了,要在每篇文章中检索名称,都会在数据库中添加一个带有上载者ID的孩子。这是我的课程文件:

public String image_thumb;
public String user_id;
public String image_url;
public String desc;

public BlogPost(){}


public BlogPost(String user_id, String image_url, String desc, String image_thumb) {
    this.user_id = user_id;
    this.image_url = image_url;
    this.desc = desc;
    this.image_thumb = image_thumb;
}



public String getUser_id() {
    return user_id;
}

public void setUser_id(String user_id) {
    this.user_id = user_id;
}

public String getImage_url() {
    return image_url;
}

public void setImage_url(String image_url) {
    this.image_url = image_url;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getImage_thumb() {
    return image_thumb;
}

public void setImage_thumb(String image_thumb) {
    this.image_thumb = image_thumb;
}
Run Code Online (Sandbox Code Playgroud)

这是我的一些适配器:

public void onBindViewHolder(final ViewHolder holder, int position) {

    String desc_data = blog_list.get(position).getDesc();
    holder.setDescText(desc_data);//this works

    String image_url = blog_list.get(position).getImage_url();
    holder.setBlogImage(image_url);//this works

    String user_id = blog_list.get(position).getUser_id();
    firebaseDatabase.child("Users").child(user_id).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                String userName = dataSnapshot.child("name").getValue().toString();

                holder.setUserName(userName);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    public void setUserName(String name){
        blogUserName = mView.findViewById(R.id.blog_user_name);
        blogUserName.setText(name);
    }
Run Code Online (Sandbox Code Playgroud)

我基本上想做的是在user_id中查找名称,并在我的名称中检索它 TextView

Ale*_*amo 8

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;}
}
Run Code Online (Sandbox Code Playgroud)

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"/>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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));
Run Code Online (Sandbox Code Playgroud)

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");
Run Code Online (Sandbox Code Playgroud)

Then you'll have to create a FirebaseRecyclerOptions object like this:

FirebaseRecyclerOptions<BlogPost> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<BlogPost>()
        .setQuery(query, BlogPost.class)
        .build();
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Then create an adapter which is declared as global:

private FirebaseRecyclerAdapter<BlogPost, BlogPostHolder> firebaseRecyclerAdapter;
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)