简体   繁体   English

recyclerview 仅显示单击项目意图的最后一项

[英]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.我正在制作一个回收器视图来显示 SQLite 数据,包括 1blob、整数主键自动增量和 3string 值,在回收器视图中带有卡片视图。 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.我已将编辑按钮放在卡片视图中,以便用户可以单击编辑按钮并编辑该 SQLite 行的详细信息。 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我通过更改按钮的 onclicklistener 得到了答案

 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.我之前的意图是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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