skip to Main Content

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


  1. Chosen as BEST ANSWER
    public class ItemDomain implements Serializable {
        private String title;
        private String address;
        private String description;
        private String pic;
        private int price;
    
    
    
        public ItemDomain() {
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public String getPic() {
            return pic;
        }
    
        public void setPic(String pic) {
            this.pic = pic;
        }
    
        public int getPrice() {
            return price;
        }
    
        public void setPrice(int price) {
            this.price = price;
        }
    
    }
    

    This is my data class


  2. add this in your onBindViewHolder

    ItemDomain itemDomain = items.get(position);
    

    then use itemDomain to get position of the item

       Intent intent = new Intent(context, DetailActivity.class);
        intent.putExtra("object", itemDomain);
        context.startActivity(intent);
    

    make sure itemDomain implements Parcelable or Serializable

    public class Item implements Parcelable {
    
    //items
    //getter/setters
    //constructor
    
    // Parcelable implementation
    protected Item(Parcel in) {
        title = in.readString();
        price = in.readDouble();
        address = in.readString();
        score = in.readInt();
    }
     public static final Creator<Item> CREATOR = new Creator<Item>() {
            @Override
            public Item createFromParcel(Parcel in) {
                return new Item(in);
        }
    
        @Override
        public Item[] newArray(int size) {
            return new Item[size];
        }
    };
    
    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(title);
        parcel.writeDouble(price);
        parcel.writeString(address);
        parcel.writeInt(score);
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search