我的结构
所以我有一个应用程序,用户在我的适配器上传帖子.我可以检索帖子描述和帖子图片,但是当我尝试检索海报的名称时,应用程序似乎崩溃,为了检索每个帖子的名称,有一个孩子使用上传者的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;}
这是我的一些适配器:
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.
假设您的活动的.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>
要使用FrIEbaseRecyclerAdapter在RecyclerVIEw中显示数据,请按照以下步骤 *** 作:
首先,您需要在活动中找到RecyclerVIEw并设置@R_403_659@,如下所示:
RecyclerVIEw recyclerVIEw = findVIEwByID(R.ID.recycler_vIEw);recyclerVIEw.setLayoutManager(new @R_403_659@(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();
在您的活动类中,创建一个如下所示的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); }}
然后创建一个声明为global的适配器:
private FirebaseRecyclerAdapter<BlogPost, BlogPostHolder> firebaseRecyclerAdapter;
并在您的活动中实例化它,如下所示:
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);
最后,不要忘记覆盖以下两种方法并开始侦听更改:
@OverrIDeprotected voID onStart() { super.onStart(); firebaseRecyclerAdapter.startListening();}@OverrIDeprotected voID onStop() { super.onStop(); if (firebaseRecyclerAdapter!= null) { firebaseRecyclerAdapter.stopListening(); }}
总结 以上是内存溢出为你收集整理的android – 如何从Firebase检索数据到我的适配器全部内容,希望文章能够帮你解决android – 如何从Firebase检索数据到我的适配器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)