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

时间:2020-10-17 12:00:18

标签: java android android-recyclerview

我正在制作一个回收站视图,以显示SQLite数据,包括1blob和整数主键自动递增以及3string值,以及在回收站视图内的卡片视图。我已将“编辑”按钮放在名片视图中,以便用户可以单击“编辑”按钮并编辑该SQLite行的详细信息。这是我的回收站视图适配器类,然后单击我有意向的其他任何具有该卡意图数据的活动卡中的“编辑”按钮。

但是我的问题是单击任何卡片项目时,它仅显示最后一项作为意图。我认为我的代码很好,

这是我的回收站适配器类

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);
        }
    }
}

我找不到我的代码发生了什么,请帮忙......

1 个答案:

答案 0 :(得分:0)

我将按钮的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);
            }
        });

 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);
            }
        });

我以前的意图不对。