0

I'm used to using populateViewHolder with firebase recycler adapter but as onBindViewHolder is the new way of doing this according to this post. I tried onBindViewHolder but it is not displaying anything. What am I doing wrong?

Here is my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_users);

mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
    mUsersList = (RecyclerView) findViewById(R.id.usersList);
    mUsersList.setHasFixedSize(true);
    mUsersList.setLayoutManager(new LinearLayoutManager(this));}


 @Override
protected void onStart() {
    super.onStart();
 FirebaseRecyclerOptions<Users> options =
          new FirebaseRecyclerOptions.Builder<Users>()
                  .setQuery(mDatabase, Users.class)
                  .build();

  FirebaseRecyclerAdapter<Users, UsersViewHolder> adapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {

      @Override
      public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

          View view = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.users_single_layout, parent, false);
          return new UsersViewHolder(view);
      }

      @Override
      protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {
            holder.setName(model.getName());
      }

  };
  mUsersList.setAdapter(adapter);

}

public static class UsersViewHolder extends RecyclerView.ViewHolder {

   View mView;
    public UsersViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setName(String name){

        TextView userNames = (TextView) mView.findViewById(R.id.textView2);
        userNames.setText(name);
    }
}}

Code of my model class.

public class Users {

public String name;
public String image;
public String status;

public Users(){


}

public Users(String name, String image, String status) {
    this.name = name;
    this.image = image;
    this.status = status;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}}

My custom layout

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">


    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/common_google_signin_btn_icon_dark_focused"
        android:id="@+id/circleImageView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="89dp"
        android:layout_marginStart="89dp"
        android:layout_toEndOf="@+id/circleImageView2"
        android:layout_toRightOf="@+id/circleImageView2"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/circleImageView2"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:text="TextView" />
</RelativeLayout>
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
notakoba
  • 160
  • 1
  • 13
  • Possible duplicate of [FirebaseListAdapter not pushing individual items for chat app - Firebase-Ui 3.1](https://stackoverflow.com/questions/47228262/firebaselistadapter-not-pushing-individual-items-for-chat-app-firebase-ui-3-1) – Peter Haddad Feb 22 '18 at 11:03
  • I saw your image below, you need to use `implementation 'com.firebaseui:firebase-ui-database:3.2.1'` then `onBindViewHolder` will work and also you need to use this https://stackoverflow.com/questions/47228262/firebaselistadapter-not-pushing-individual-items-for-chat-app-firebase-ui-3-1 – Peter Haddad Feb 22 '18 at 19:41

2 Answers2

0

Make sure you set read rules in firebase project. Set read to true.

Check rules in firebase project:

enter image description here

M123
  • 1,203
  • 4
  • 14
  • 31
b8x
  • 88
  • 1
  • 10
-1

You can add:

 adapter.startListening();

after:

mUsersList.setAdapter(adapter);

And it should look like:

mUsersList.setAdapter(adapter);

adapter.startListening();
Mhamza007
  • 151
  • 1
  • 4