英文:
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 <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......
答案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("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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论