gpt4 book ai didi

android - 如何将数据从 Firebase 检索到我的适配器

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:39:11 32 4
gpt4 key购买 nike

我的结构

enter image description here

所以我有一个应用程序,用户可以在其中上传帖子到我的适配器中。我可以检索帖子描述和帖子图片,但是当我尝试检索发布者的姓名时,应用程序似乎崩溃了,要检索每个帖子的名称,都会将一个 child 添加到数据库中,其中包含上传者的 ID。这是我的类(class)文件:

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;
}

这是我的一些适配器:

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);
}

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

中检索它

最佳答案

为了使其正常工作,我建议您对模型类和代码进行一些更改。您的模型类应该如下所示:

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;}
}

请参阅字段和 getter 的命名约定。

In order to make it work, don't forget the remove the old data and add fresh one.

假设您的 Activity 有一个 .XML 文件,其中包含如下所示的 RecyclerView:

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

还有一个用于项目文件的 .XML 文件,如下所示:

<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>

要使用 FriebaseRecyclerAdapterRecyclerView 中显示您的数据,请按照以下步骤操作:

首先,您需要在您的 Activity 中找到 RecyclerView 并像这样设置 LinearLayoutManager:

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

然后您需要创建 Firebase 数据库的根引用和一个 Query 对象,如下所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Users");

然后你必须像这样创建一个 FirebaseRecyclerOptions 对象:

FirebaseRecyclerOptions<BlogPost> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<BlogPost>()
.setQuery(query, BlogPost.class)
.build();

在您的 Activity 类中,创建一个如下所示的 holder 类:

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);
}
}

然后创建一个声明为全局的适配器:

private FirebaseRecyclerAdapter<BlogPost, BlogPostHolder> firebaseRecyclerAdapter;

然后像这样在您的 Activity 中实例化它:

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);

最后不要忘记重写下面两个方法,开始监听变化:

@Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}

@Override
protected void onStop() {
super.onStop();

if (firebaseRecyclerAdapter!= null) {
firebaseRecyclerAdapter.stopListening();
}
}

关于android - 如何将数据从 Firebase 检索到我的适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49383687/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com