简体   繁体   中英

recyclerview shows only last item on click an item intent

I am making a recycler view to display SQLite data including 1blob, and integer primary key autoincrement, and 3string values with card views inside recycler view. I've put the edit button in the card view so that the user can click the edit button and edit the details of that SQLite row. This is my recycler view adapter class, then clicking the edit button from any card I made an intent to another activity with intent data from that card.

But my problem is when clicking any card item, it shows only the last item as the intent. I think my code is fine,

This is my recycler adapter class

public class RvadapterMyItems extends RecyclerView.Adapter <RvadapterMyItems.MyViewHolder> {

   private Context context;
   Activity activity;
   MyDatabaseHelper myDB;
   InterstitialAd interstitialAd;
   String myitemid, myitemname, myitemprice, myiteminstock, currencystring;
   SharedPreferences share2;
    private static final int EDIT_MY_ITEM = 111;
    byte[] bytes;

   private ArrayList arid, armyitempic, armyitemname, armyitemprice, armyiteminstock;
     int position;
    RvadapterMyItems(Activity activity, Context context, ArrayList arid, ArrayList armyitempic, ArrayList armyitemname, ArrayList armyitemprice, ArrayList armyiteminstock){
        this.activity = activity;
        this.context = context;
        this.arid = arid;
       this.armyitempic = armyitempic;
       this.armyitemname = armyitemname;
       this.armyitemprice = armyitemprice;
       this.armyiteminstock = armyiteminstock;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
      View view =   inflater.inflate(R.layout.cardlayoutmyitems, parent, false);
        return new MyViewHolder(view);
    }



    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull  MyViewHolder holder, int position) {
       
 share2 = context.getSharedPreferences("shopinfo", MODE_PRIVATE);
        if(share2.contains("currency")){
            currencystring = share2.getString("currency", "");
            assert currencystring != null;
            if(currencystring.equals("No")){
                currencystring = "";
            }
        }else{
            currencystring = "";
        }
            myitemid = String.valueOf(arid.get(position));
            myitemname = String.valueOf(armyitemname.get(position));
            myitemprice = String.valueOf(armyitemprice.get(position)+ " "+ currencystring);
            myiteminstock = String.valueOf("Instock: "+ String.valueOf(armyiteminstock.get(position))+ " pcs");

        Bitmap bitmap = DbBitmapUtility.getImage((byte[]) armyitempic.get(position));
        holder.ivmyitem.setImageBitmap(bitmap);
        holder.tvmyitemname.setText(myitemid+ " / "+myitemname);
        holder.tvmyitemprice.setText(myitemprice);
        holder.tvmyiteminstock.setText(myiteminstock);
        bytes = DbBitmapUtility.getBytes(bitmap);

      holder.bteditmyitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              Intent intent = new Intent(activity, AddMyItem.class);
                intent.putExtra("itemid",myitemid);
                intent.putExtra("itempic",bytes);
                intent.putExtra("itemname",myitemname);
                intent.putExtra("itemprice",myitemprice);
                intent.putExtra("iteminstock", myiteminstock);
                activity.startActivityForResult(intent,1);
            }
        });
    }
    @Override
    public int getItemCount() {
        return arid.size();
    }
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvmyitemname, tvmyitemprice, tvmyiteminstock;
        ImageView ivmyitem;
        ImageButton bteditmyitem;
        ConstraintLayout mainlayoutmyitem;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
         ivmyitem = itemView.findViewById(R.id.ivmyitem);
         tvmyitemname = itemView.findViewById(R.id.tvmyitemname);
         tvmyitemprice = itemView.findViewById(R.id.tvmyitemprice);
         tvmyiteminstock = itemView.findViewById(R.id.tvmyiteminstock);
         bteditmyitem = itemView.findViewById(R.id.bteditmyitem);
        }
    }
}

I can't find what happened to my code, please help......

I got the answer by changing the onclicklistener of button from

 holder.bteditmyitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              Intent intent = new Intent(activity, AddMyItem.class);
                intent.putExtra("itemid",myitemid);
                intent.putExtra("itempic",bytes);
                intent.putExtra("itemname",myitemname);
                intent.putExtra("itemprice",myitemprice);
                intent.putExtra("iteminstock", myiteminstock);
                activity.startActivityForResult(intent,1);
            }
        });

to

 holder.bteditmyitem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             Intent intent = new Intent(activity, AddMyItem.class);
                intent.putExtra("itemid",String.valueOf(arid.get(position)));
                intent.putExtra("itempic",(byte[]) armyitempic.get(position));
                intent.putExtra("itemname",String.valueOf(armyitemname.get(position)));
                intent.putExtra("itemprice",String.valueOf(armyitemprice.get(position)));
               // intent.putExtra("iteminstock", myiteminstock);
                activity.startActivityForResult(intent,1);
            }
        });

I was wrong in getting intent previously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM