The recyclerView shows a single item when an authenticated user logs in. The authenticated user has to see different photos i.e if they have uploaded different photos.
The MainActivity gets data from firestore and is stored in an array list which is passed to the other arraylist in the adapter.
Below is the MainActivity Code Sample that gets data from firestore and stores in arrayList
fStore.collection("images").whereEqualTo("userId", Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid()).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
// DocumentSnapshot document = task.getResult();
Item item = new Item();
itemList = new ArrayList<>();
for(QueryDocumentSnapshot document: task.getResult()){
item.setItemName(document.getData().get("name").toString());
item.setItemPrice(document.getData().get("price").toString());
item.setItemImage(document.getData().get("image").toString());
item.setItemDescription(document.getData().get("description").toString());
itemList.add(item);
System.out.println("This are my items:"+ itemList.get(0).getItemName());
}
shoeAdapter = new ShoeAdapter(MainActivity.this, itemList);
recyclerView.setAdapter(shoeAdapter);
shoeAdapter.notifyDataSetChanged();
}else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
The Adapter Code Sample below
package com.bac.shoesrecyclerview;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.List;
public class ShoeAdapter extends RecyclerView.Adapter<ShoeAdapter.ViewHolder> {
private Context context;
private List<Item> mItems;
public ShoeAdapter(Context context, List<Item> items) {
this.mItems = items;
this.context = context;
}
public void removeItem(int position){
mItems.remove(position);
notifyItemRemoved(position);
}
public void restoreItem(Item shoes, int position){
mItems.add(position, shoes);
notifyItemInserted(position);
}
@NonNull
@Override
public ShoeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.item_layout, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ShoeAdapter.ViewHolder holder, int position) {
Item upload = mItems.get(position);
Glide.with(context).load(upload.getItemImage()).into(holder.imageView);
System.out.println("ITEM ARRAY:"+ upload);
holder.nameTextView.setText(upload.getItemName());
holder.nameTextView2.setText(upload.getItemDescription());
holder.textViewStock.setText(upload.getItemPrice());
}
@Override
public int getItemCount() {
return mItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView nameTextView;
public TextView nameTextView2;
public TextView textViewStock;
public ImageView imageView;
public RelativeLayout displayLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.textViewDescription);
nameTextView2 = itemView.findViewById(R.id.textViewName);
imageView = itemView.findViewById(R.id.imageView);
textViewStock = itemView.findViewById(R.id.textViewStock);
displayLayout = itemView.findViewById(R.id.layoutDisplay);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
Item items = mItems.get(position);
Intent intent = new Intent(context,display_shoes.class);
intent.putExtra("name",items.getItemName());
intent.putExtra("description",items.getItemDescription());
intent.putExtra("image",items.getItemImage());
intent.putExtra("price",items.getItemPrice());
context.startActivity(intent);
}
}
}
I debugged the above adapter by printing what upload variable hasItem upload = mItems.get(position); System.out.println("ITEM ARRAY:"+ upload);
.
Even though it does not hold any readable and understandable values it is showing repetition am suspecting that might be the root cause.
The Layout of the Item activity That gets Inflated
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/white">
<RelativeLayout
android:id="@+id/view_background"
android:layout_width="match_parent"
android:layout_height="244dp"
android:background="#D81B60">
<ImageView
android:id="@+id/delete_icon"
android:layout_width="85dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginStart="50dp"
android:layout_marginRight="15dp"
android:src="@drawable/ic_baseline_delete_24" />
<TextView
android:layout_width="52dp"
android:layout_height="165dp"
android:layout_centerVertical="true"
android:layout_marginStart="90dp"
android:layout_marginEnd="-100dp"
android:layout_toLeftOf="@id/delete_icon"
android:text="Delete"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold" />
<RelativeLayout
android:id="@+id/layoutDisplay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:padding="10dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginRight="10dp"
android:background="@drawable/roundshape"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView"
android:ellipsize="end"
android:fontFamily="sans-serif"
android:maxLines="1"
android:textColor="@color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/textViewDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewName"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/imageView"
android:textColor="#555353"
android:textSize="12sp" />
<TextView
android:id="@+id/textViewStock"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="30dp"
android:layout_marginBottom="140dp"
android:layout_toRightOf="@+id/imageView"
android:textColor="#D81B60"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
The Structure Of The RecyclerView
<?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"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="83dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
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="parent"
app:layout_constraintVertical_bias="0.0" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/logOutBtn"
android:layout_width="85dp"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/roundcheck"
app:layout_constraintEnd_toEndOf="@+id/toolbar"
app:layout_constraintHorizontal_bias="0.049"
app:layout_constraintStart_toStartOf="@+id/toolbar"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="@+id/imageView6"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="28dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="@+id/logOutBtn"
app:layout_constraintStart_toEndOf="@+id/logOutBtn"
app:srcCompat="@drawable/activeperson" />
<TextView
android:id="@+id/LoggedIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="LoggedIn :"
android:textColor="#F6F2F2"
android:textSize="14sp"
app:layout_constraintStart_toEndOf="@+id/imageView6"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="4dp"
android:backgroundTint="@color/light_pink"
android:src="@drawable/ic_baseline_add_box_24" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="90dp" />
</RelativeLayout>