如何从Firebase检索数据到适配器我有一个应用程序,用户可以在我的适配器中上传帖子。我可以检索帖子描述和帖子图片,但当我试图检索海报的名称时,应用程序似乎崩溃了,为了检索每个帖子的名称,在数据库中添加了一个带有上传者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
添加回答
举报
0/150
提交
取消