Ale4303
Ale4303

Reputation: 459

Recyclerview empty even though I populated it

My recyclerview is empty, even tough i populated it. I'm very certain that the list has data, as was confirmed by the debugger.

Adapter:

private StorageReference currentProfilepicture;
private Glide glide;
private List<Friendmodel> friendList;
private Context context;
private StorageReference storageReference;
private CollectionReference collectionReference;



public FriendsAdapter(Context context, StorageReference storageReference, CollectionReference collectionReference, List<Friendmodel> userList) {
    this.context = context;
    this.storageReference = storageReference;
    this.friendList = userList;
    this.collectionReference = collectionReference;
}



@NonNull
@Override
public CategoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.friendsrow, parent, false);

    return new CategoryViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull CategoryViewHolder categoryViewHolder, int i) {
    Friendmodel friendmodel = friendList.get(categoryViewHolder.getAdapterPosition());
    currentProfilepicture = storageReference.child("images/" + friendmodel.getUserid());
    glide.with(context)
            .using(new FirebaseImageLoader())
            .load(currentProfilepicture)
            .into(categoryViewHolder.Profilepictureholder);

    collectionReference.document(friendmodel.getUserid());
    categoryViewHolder.Usernameholder.setText(friendmodel.getUsername());

}

@Override
public int getItemCount() {
    return 0;
}

class CategoryViewHolder extends RecyclerView.ViewHolder {


    public TextView Usernameholder;
    public CircleImageView Profilepictureholder;
    public CardView Cardviewholder;






    public CategoryViewHolder(@NonNull View itemView) {
        super(itemView);
        Usernameholder = (TextView) itemView.findViewById(R.id.username);
        Profilepictureholder = (CircleImageView) itemView.findViewById(R.id.circleimageview);
        Cardviewholder = (CardView) itemView.findViewById(R.id.Cardviewofcircleimageview);



    }


}

Recyclerview:

    //SETUP RECYCLERVIEW AND PARAMETERS:
    mRecyclerView = rootView.findViewById(R.id.recyclerviewfriends);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    CollectionReference collectionReference = db.collection("users");
    Context context = getActivity();
    storageReference = FirebaseStorage.getInstance().getReference();

    //QUERY SET TO ORDER ALL OF THE USERS FRIENDS BY USERNAME --> RETURN QUERY WITH ALL FRIENDELEMENTS:
    com.google.firebase.firestore.Query Friendsquery = db.collection("users").document(uid).collection("Friends").orderBy("username");


    //CONVERT ALL ELEMENTS FROM QUERY INTO A FRIENDMODEL AND ADD IT TO USERLIST:

    HashMap<String, String> data = new HashMap<>();


    Friendsquery.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e)
        {
            List<Friendmodel> userList = queryDocumentSnapshots.toObjects(Friendmodel.class);

            //CREATE NEW ADAPTER WITH ALL THE USERS
            FriendsAdapter adapter = new FriendsAdapter(context,storageReference, collectionReference, userList );
            mRecyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    });

Rowview:

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

    <androidx.cardview.widget.CardView
        app:cardCornerRadius="16dp"
        android:id="@+id/Cardview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="7dp"
        android:layout_marginBottom="7dp"
        android:layout_alignParentTop="true"
        >

        <RelativeLayout
            android:id="@+id/relativelayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp">

            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Cardviewofcircleimageview"
                android:layout_margin="3dp"
                app:cardCornerRadius="20dp"
                app:cardElevation="1dp"

                >

                <de.hdodenhof.circleimageview.CircleImageView
                    android:id="@+id/circleimageview"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_margin="7dp"
                    android:cropToPadding="true"
                    android:src="@drawable/cutenessoverload1" />


            </androidx.cardview.widget.CardView>

            <TextView
                android:id="@+id/username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="7dp"
                android:layout_toEndOf="@id/Cardviewofcircleimageview"
                android:text="12345678910"
                android:textSize="25sp"
                android:textStyle="bold" />

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/floatingActionButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"
                android:layout_marginEnd="16dp"
                android:src="@drawable/ic_chat_bubble_outline_black_24dp" />


        </RelativeLayout>
    </androidx.cardview.widget.CardView>


</RelativeLayout>

Recycler layout:

<?xml version="1.0" encoding="utf-8"?>

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:drawableRight="@drawable/ic_chat_bubble_outline_black_24dp"
                android:text="Chats"
                android:maxLines="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Chats"
                android:textSize="40sp"
                android:textStyle="bold"
                android:textAlignment="center"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="25dp"
                android:layout_marginBottom="15dp">

            </TextView>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerviewchat"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/Chats"
                android:visibility="visible" />


            <TextView
                android:layout_below="@id/recyclerviewchat"
                android:maxLines="1"
                android:drawableRight="@drawable/ic_people_black_24dp"
                android:text="Friends"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/Friends"
                android:textSize="40sp"
                android:textStyle="bold"
                android:textAlignment="center"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="25dp"
                android:layout_marginBottom="15dp">

            </TextView>

            <androidx.recyclerview.widget.RecyclerView
                android:layout_below="@id/Friends"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/recyclerviewfriends"
                android:visibility="visible"
                />

        </RelativeLayout>

    </ScrollView>

I don't know what the issue could be, maybe it has to do with the recyclerview being inside a fragment or maybe there is an UI issue with the recycler layout.

Upvotes: 0

Views: 75

Answers (2)

Ravindra Kushwaha
Ravindra Kushwaha

Reputation: 8042

Because you are returning the 0 size of the Recyclview's getItemCount() of the Adapter , therefore you are getting empty List

@Override
 public int getItemCount() {
    return 0;
 }

You just need to return , your ArrayList size here , like below

@Override
 public int getItemCount() {
     return userList.size();  ///userList is your `ArrayList`
 }

Upvotes: 1

furkanbzkurt
furkanbzkurt

Reputation: 356

@Override
public int getItemCount() {
  return 0;
}

replace this part with:

@Override
public int getItemCount() {
  return friendList == null ? 0 : friendList.size();
}

Upvotes: 1

Related Questions