即使我填充了Recyclerview也为空

时间:2019-03-26 13:55:16

标签: android android-recyclerview

我的recyclerview是空的,即使我填充它也很困难。我非常确定列表中有数据,正如调试器确认的那样。

适配器

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);



    }


}

回收站视图:

    //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();
        }
    });

行列:

<?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>

回收站布局:

<?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>

我不知道问题可能是什么,也许与recyclerview位于片段中有关,或者recycler布局存在UI问题。

2 个答案:

答案 0 :(得分:1)

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

将此部分替换为:

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

答案 1 :(得分:1)

由于您要返回适配器的size的{​​{1}}的 0 Recyclview,因此您将得到空的getItemCount() < / p>

List

您只需要在此处返回@Override public int getItemCount() { return 0; } 的大小,如下所示

ArrayList