My structure
So I have an app in which users upload posts in my adapter. I can retrieve the post description and the post picture, but when I try to retrieve the poster's name the app seems to crash, to retrieve the name with every post there is a child added to the database with the uploader's id. This is my class file:
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;
}
and this is some of my adapter:
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);
}
What I basically wanna do is look inside the user_id for the name and retrieve it inside my TextView