How can I retrieve data from Firebase to my adapter

前端 未结 3 630
南旧
南旧 2020-11-21 06:29

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 retriev

3条回答
  •  鱼传尺愫
    2020-11-21 07:00

    Its better to not include any firebase calls inside your ViewBinder for recycler view. What you can do is update your BlogPost and include a field name with getter and setter. Then inside your activity where you are adding the BlogPost to the recycler adapter you can fetch the user name add the name to the blog post and notify adapter of data change.

    In your activity do this

    // adding BlogPost to list
    // Create BlogPost object with the data.
    blogList.add(blogPostObject)
    firebaseDatabase.child("Users").child(user_id).child("name").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                  blogPostObject.setName(dataSnapshot.getValue().toString());
                  adapter..notifyItemChanged(indexOfblogPostObject);
                }
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    

提交回复
热议问题