I’m experiencing an issue with my RecyclerView adapter. When I have fewer than 5 items in my Firebase database, the adapter works perfectly, and tapping an item displays the correct details. However, when I add more items (beyond 5), the adapter starts behaving erratically. Tapping an item displays the details of a different item, not the one I tapped. I suspect the issue lies within my adapter implementation. Can anyone help me identify the problem and suggest a solution?
The Recyclerview adapter is not working as intended here is it
package com.app.rednet.myspaceapp.Adapter;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.app.rednet.myspaceapp.Activity.DetailActivity;
import com.app.rednet.myspaceapp.Domain.ItemDomain;
import com.app.rednet.myspaceapp.databinding.ViewholderRecommendedBinding;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class RecommendedAdapter extends RecyclerView.Adapter<RecommendedAdapter.Viewholder> {
private final ArrayList<ItemDomain> items;
private Context context;
ViewholderRecommendedBinding binding;
public RecommendedAdapter(ArrayList<ItemDomain> items) {
this.items = items;
this.context = null;
}
@NonNull
@Override
public RecommendedAdapter.Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
binding=ViewholderRecommendedBinding.inflate(LayoutInflater.from(viewGroup.getContext()),viewGroup,false);
context=viewGroup.getContext();
return new Viewholder(binding);
}
@Override
public void onBindViewHolder(@NonNull RecommendedAdapter.Viewholder viewholder, int i) {
binding.titleTxt.setText(items.get(i).getTitle());
binding.priceTxt.setText("MWK"+items.get(i).getPrice());
binding.addressTxt.setText(items.get(i).getAddress());
binding.scoreTxt.setText(""+items.get(i).getScore());
Glide.with(context)
.load(items.get(i).getPic())
.into(binding.pic);
viewholder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = viewholder.getBindingAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
Intent intent = new Intent(context, DetailActivity.class);
ItemDomain item = items.get(position);
intent.putExtra("object", item);
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return items.size();
}
public class Viewholder extends RecyclerView.ViewHolder {
public Viewholder(ViewholderRecommendedBinding binding) {
super(binding.getRoot());
}
}
}
2
Answers
This is my data class
add this in your onBindViewHolder
then use
itemDomain
to get position of the itemmake sure itemDomain implements Parcelable or Serializable