RecyclerView only lists first item despite layout changes

Viewed 37

I understand this might be a duplicate, but every other solution here didn't work in my case, such as changing the layout_height to wrap_content instead of match_parent.

My Firestore database has 2 values stored inside an array, and I want to display both of them (more in the future) in my RecyclerView, however, it only displays the first value, at position 0.

Here is my Activity where the Recycler and the Adapter are created:

ActivityOs

 private FirestoreAdapter listAdapter;
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference osRef = db.collection("cliente");
    private DocumentReference docRef = osRef.document("clienteTeste");

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

        RecyclerView recyclerView = findViewById(R.id.recyclerViewOs);

        /*docRef.get().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    Log.e("RESULTADO","result:" + document.toString());
                }
            }
        });*/

        Query query = osRef.orderBy("serviceOrder",
                Query.Direction.ASCENDING);

        FirestoreRecyclerOptions<ServiceDocument> options =
                new FirestoreRecyclerOptions.Builder<ServiceDocument>()
                        .setQuery(osRef, ServiceDocument.class)
                        .build();

         listAdapter = new FirestoreAdapter(options);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(listAdapter);
        listAdapter.notifyDataSetChanged();

    }

    @Override
    protected void onStart() {
        super.onStart();
        listAdapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        listAdapter.stopListening();
    }

The commented code shows me that the database does indeed return the 2 values stored in it.

Here is my Adapter class:


public class FirestoreAdapter extends FirestoreRecyclerAdapter<ServiceDocument, FirestoreAdapter.ViewHolder> {

    public FirestoreAdapter(@NonNull FirestoreRecyclerOptions<ServiceDocument> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull ServiceDocument model) {
        holder.osIdItem.setText(String.valueOf(model.serviceOrderArray.get(position).getId()));
        holder.osClientItem.setText(model.serviceOrderArray.get(position).getClient().getName());
        holder.osDateItem.setText(model.serviceOrderArray.get(position).getDate());
        holder.osValueItem.setText(String.valueOf(model.serviceOrderArray.get(position).getTotalValue()));
    }

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

        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());

        View view = layoutInflater.inflate(R.layout.layout_os_item, parent, false);

        return new FirestoreAdapter.ViewHolder(view);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private  TextView osIdItem;
        private  TextView osClientItem;
        private  TextView osDateItem;
        private  TextView osValueItem;

        public ViewHolder(View itemView) {
            super(itemView);

            osIdItem =  itemView.findViewById(R.id.osIDItem);
            osClientItem =   itemView.findViewById(R.id.osClientNameItem);
            osDateItem =  itemView.findViewById(R.id.osDateItem);
            osValueItem =  itemView.findViewById(R.id.osValueItem);
        }
    }
}

And here are my layouts, activity_os.xml where my RecyclerView is created, and layout_os_item.xml where the layouts for each item of the recycler are made.

Activity_os.xml

 <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/activity_os"
        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="wrap_content"
        android:layout_marginBottom="0dp"
        android:background="@color/white"
        tools:context="com.leonardomaito.autocommobile.activities.OsActivity">

        <include
            android:id="@+id/include"
            layout="@layout/layout_header_search" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/glRecyclerLimit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_begin="124dp" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerViewOs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="650dp"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/glRecyclerLimit"
            app:layout_constraintVertical_bias="0.0"
            tools:listitem="@layout/layout_os_item" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/btNewOs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="Inserir OS"
            android:onClick="createNewOs"
            android:src="@drawable/custom_plus_icon"
            app:backgroundTint="@color/autocom_blue"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.98"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/glRecyclerLimit"
            app:layout_constraintVertical_bias="0.988"
            app:rippleColor="@color/autocom_blue" />

    </androidx.constraintlayout.widget.ConstraintLayout>

Layout_os_item.xml

<androidx.cardview.widget.CardView
    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="wrap_content"
    tools:showIn="@layout/activity_os"
    android:background="@drawable/custom_card_view">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="10dp"
        android:background="@drawable/custom_card_view">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/custom_os_icon"
            android:paddingLeft="5dp"/>

        <TextView
            android:id="@+id/osIDItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="N 00000"
            android:textColor="@color/autocom_blue"
            android:textStyle="bold"
            app:layout_constraintEnd_toStartOf="@+id/osDateItem"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/osDateItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/autocom_blue"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.943"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.222" />

        <TextView
            android:id="@+id/osClientNameItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:textColor="@color/autocom_blue"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@+id/imageView"
            app:layout_constraintTop_toBottomOf="@+id/osIDItem"
            app:layout_constraintVertical_bias="0.111" />

        <TextView
            android:id="@+id/osValueItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:text="R$ 10,000"
            android:textColor="@color/autocom_blue"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.791"
            app:layout_constraintStart_toEndOf="@+id/osClientNameItem"
            app:layout_constraintTop_toBottomOf="@+id/osDateItem"
            app:layout_constraintVertical_bias="0.0" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Any help is welcome!

Edit

Current Result:

Result

Expected Result

Expected

Basically, Item 0 or the first item is correct, however, the second item in my database should also appear in Item 1.

0 Answers
Related