Sergey
Sergey

Reputation: 35

RecyclerView shows only one item

Im trying to display info from firebase firestore, but I get only one record in Recycleview. I do not know the reason of this. I have multiple records in my firestore collection, but only first one in list is returned/displayed.

public class fragment_history extends Fragment {
private final String TAG = "err";
private FirestoreRecyclerAdapter adapter;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_history, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    
    RecyclerView firestoreList = view.findViewById(R.id.recycler_view);
    
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    String userId = Objects.requireNonNull(user).getUid();
    
    Query query = firebaseFirestore.collection("user").document(userId).collection("prod1").limit(10);
    FirestoreRecyclerOptions<ProductModel> options = new FirestoreRecyclerOptions.Builder<ProductModel>()
            .setQuery(query, ProductModel.class)
            .build();
    
    adapter = new FirestoreRecyclerAdapter<ProductModel, ProductViewHolder>(options) {
        @NonNull
        @Override
        public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.display_item, parent, false);
            return new ProductViewHolder(view);
        }

        @SuppressLint("SetTextI18n")
        @Override
        protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull ProductModel model) {
            holder.product_name.setText(model.getName());
            holder.product_price.setText(model.getPrice() + "");
        }
    };

    firestoreList.setHasFixedSize(true);
    firestoreList.setAdapter(adapter);

}
private static class ProductViewHolder extends RecyclerView.ViewHolder {
    private final TextView product_name;
    private final TextView product_price;

    public ProductViewHolder(@NonNull View itemView) {
        super(itemView);

        product_name = itemView.findViewById(R.id.product_name);
        product_price = itemView.findViewById(R.id.product_price);
    }

}

@Override
public void onStop() {
    super.onStop();
    adapter.stopListening();
}

@Override
public void onStart() {
    super.onStart();
    adapter.startListening();
}
}

LIST XML

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <androidx.recyclerview.widget.RecyclerView
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recycler_view" />

</RelativeLayout>

ITEM XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/product_name"
    android:text="item1name"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/product_price"
    android:text="item1price"/>

I though this was because of list xml layout_height, but if I set it to wrap_content, then list is empty. Can you please help?

Upvotes: 1

Views: 165

Answers (1)

Mohit Ajwani
Mohit Ajwani

Reputation: 1338

The item.xml has LinearLayout height like match_parent android:layout_height="match_parent" which is wrong. Change it to wrap_content and it will solve the problem.

Upvotes: 3

Related Questions