RecyclerView 在点击项目意图后只显示最后一个项目

huangapple go评论70阅读模式
英文:

recyclerview shows only last item on click an item intent

问题

以下是翻译好的代码部分:

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 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 &lt;RvadapterMyItems.MyViewHolder&gt; {
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(&quot;SetTextI18n&quot;)
@Override
public void onBindViewHolder(@NonNull  MyViewHolder holder, int position) {
share2 = context.getSharedPreferences(&quot;shopinfo&quot;, MODE_PRIVATE);
if(share2.contains(&quot;currency&quot;)){
currencystring = share2.getString(&quot;currency&quot;, &quot;&quot;);
assert currencystring != null;
if(currencystring.equals(&quot;No&quot;)){
currencystring = &quot;&quot;;
}
}else{
currencystring = &quot;&quot;;
}
myitemid = String.valueOf(arid.get(position));
myitemname = String.valueOf(armyitemname.get(position));
myitemprice = String.valueOf(armyitemprice.get(position)+ &quot; &quot;+ currencystring);
myiteminstock = String.valueOf(&quot;Instock: &quot;+ String.valueOf(armyiteminstock.get(position))+ &quot; pcs&quot;);
Bitmap bitmap = DbBitmapUtility.getImage((byte[]) armyitempic.get(position));
holder.ivmyitem.setImageBitmap(bitmap);
holder.tvmyitemname.setText(myitemid+ &quot; / &quot;+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(&quot;itemid&quot;,myitemid);
intent.putExtra(&quot;itempic&quot;,bytes);
intent.putExtra(&quot;itemname&quot;,myitemname);
intent.putExtra(&quot;itemprice&quot;,myitemprice);
intent.putExtra(&quot;iteminstock&quot;, 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......

答案1

得分: 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);
}
});

之前获取意图的方式是错误的。

英文:

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(&quot;itemid&quot;,myitemid);
intent.putExtra(&quot;itempic&quot;,bytes);
intent.putExtra(&quot;itemname&quot;,myitemname);
intent.putExtra(&quot;itemprice&quot;,myitemprice);
intent.putExtra(&quot;iteminstock&quot;, 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(&quot;itemid&quot;,String.valueOf(arid.get(position)));
intent.putExtra(&quot;itempic&quot;,(byte[]) armyitempic.get(position));
intent.putExtra(&quot;itemname&quot;,String.valueOf(armyitemname.get(position)));
intent.putExtra(&quot;itemprice&quot;,String.valueOf(armyitemprice.get(position)));
// intent.putExtra(&quot;iteminstock&quot;, myiteminstock);
activity.startActivityForResult(intent,1);
}
});

I was wrong in getting intent previously.

huangapple
  • 本文由 发表于 2020年10月17日 20:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64402217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定