gpt4 book ai didi

java - 如何在单击按钮时更改列表项中的 textView

转载 作者:行者123 更新时间:2023-11-30 01:54:06 25 4
gpt4 key购买 nike

我想更改我的数量 TextView 以反射(reflect)特定项目的数量。但是在单击添加和子按钮时,setText() 函数似乎不起作用。这是我的适配器类

public class ItemAdapter extends ArrayAdapter<String>{
Context context;
int[] images,rate,quant;
String[] names;
ImageButton add, sub;
ViewHolder holder; // Since will be accessed inside onclick
Integer count1=0;

HashMap<String,Integer> positiveNumbers = new HashMap<String,Integer>();
Integer count=0;

static class ViewHolder {
public TextView textView;
public ImageView imageView;
public TextView number;
public TextView rates;
public String uniqueKey;

}

public ItemAdapter(Context c, String[] items, int quantity[], int[] rate, int[] img) {
// TODO Auto-generated constructor stub
super(c, R.layout.item_fooditems, R.id.tvitemname, items);
this.context = c;
this.images = img;
this.names = items;
this.rate=rate;
this.quant=quantity;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
// reuse views

if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.item_fooditems, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) row.findViewById(R.id.ivimage);
viewHolder.textView = (TextView) row.findViewById(R.id.tvitemname);
viewHolder.number = (TextView)row.findViewById(R.id.tvquantity);
viewHolder.rates = (TextView)row.findViewById(R.id.tvprice);
viewHolder.uniqueKey = String.valueOf(position);
add = (ImageButton) row.findViewById(R.id.bincrement);
add.setTag(position);
sub = (ImageButton) row.findViewById(R.id.bdecrement);
sub.setTag(position);
row.setTag(viewHolder);

}
holder = (ViewHolder) row.getTag(); //keeping one global memory
holder.imageView.setImageResource(images[position]);
holder.textView.setText(names[position]);
holder.rates.setText(Integer.toString(rate[position]));
holder.number.setText(Integer.toString(quant[position]));
holder.number.setText("x");
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
quant[position]=quant[position]+1;
count1 +=1;
//holder.number.setText(Integer.toString(quant[position]));
holder.number.setText(count1.toString());
Toast.makeText(context, Integer.toString(quant[position]), Toast.LENGTH_SHORT).show();
Log.d("Quantity",Integer.toString(quant[position]));
positiveNumbers.put(holder.uniqueKey,count); //Key -> String.valueOf(position) and Value -> int count
}
});

sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
quant[position]--;
Toast.makeText(context, Integer.toString(quant[position]), Toast.LENGTH_SHORT).show();
holder.number.setText(Integer.toString(quant[position]));
positiveNumbers.put(holder.uniqueKey,count); //Key -> String.valueOf(position) and Value -> int count
}
});

return row;
}


public HashMap<String, Integer> getPositiveNumbers()
{
return positiveNumbers;
}


}

需要进行哪些修改?

最佳答案

首先,不要使用这些单独的数组,您应该简单地创建一个包含您需要的所有值的自定义模态类。例如,在您的情况下,它将是这样的。

    public class Item {

private String itemName = "";
private int quantity;
private int rate;
private int image;

public String getItemName() {
return itemName;
}

public void setItemName(String itemName) {
this.itemName = itemName;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public int getRate() {
return rate;
}

public void setRate(int rate) {
this.rate = rate;
}

public int getImage() {
return image;
}

public void setImage(int image) {
this.image = image;
}
}

现在将这个模态类传递给您的适配器构造函数。这就是您可以在 getView() 中设置值的方式

    Item item=itemList.get(position); //here itemList is the list you are passing to your adapter.
holder.imageView.setImageResource(item.getImage());
holder.textView.setText(item.getItemName());
holder.rates.setText(String.valueOf(item.getRate()));
holder.number.setText(String.valueOf(item.getQuantity()));

现在来到您的添加/子点击监听器,无需在添加/子点击监听器中设置您的数量 TextView 的文本。您可以在更新 itemList 相应索引处的数量值后简单地通知您的适配器。例如。要增加数量,您可以这样做。

add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Item item=itemList.get(position);// getting the item at particular position
item.setQuantity(item.getQuantity()+1);// incrementing item quantity by 1
itemList.set(position,item);// updating the itemList to that new item with incremented quantity
notifyDataSetChanged();//notifying the adapter
}
});

您可以对减去数量的 onClickListener() 执行相同的操作。

sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Item item=itemList.get(position);//getting the item at particular position
item.setQuantity(item.getQuantity()-1);//decrementing item quantity by 1
itemList.set(position,item);// updating the itemList to that new item with decremented quantity
notifyDataSetChanged();//notifying the adapter
}
});

这肯定会奏效。在处理 ListView/RecyclerView 的自定义适配器时,维护单个列表而不是多个数组也是一种很好的做法。

关于java - 如何在单击按钮时更改列表项中的 textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32539267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com