如何在android中增加视图持有者变量的值? [英] How to increment a value on view holder variables in android?
本文介绍了如何在android中增加视图持有者变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 Android 应用中有一个 viewholder
,我试图在文本字段上设置一个递增的值.我试过如下,
I have a viewholder
in my Android app and I tried to set an incremented value on a textfield. I tried it like following,
活动
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new Holder();
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.menu_list_item, null);
holder.textViewItemName = (TextView) convertView.findViewById(R.id.textViewItemName);
holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewItem);
holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
holder.cartQtyTextView = (TextView) convertView.findViewById(R.id.textViewCartQty);
holder.buttonMinus = (ButtonRectangle) convertView.findViewById(R.id.buttonMinus);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
final MenuItem listItem = objects.get(position);
holder.textViewItemName.setText(listItem.getItemName());
holder.textViewPrice.setText("$ ".concat(String.valueOf(listItem.getItemPrice())));
// Check & Set
if (holder.buttonPlus != null) {
holder.buttonPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.cartQtyTextView != null) {
if (holder.inc >= 0) {
holder.cartQtyTextView.setText(String.valueOf(holder.inc++));
}
}
}
});
}
holder.buttonPlus.setBackgroundColor(Color.WHITE);
holder.buttonPlus.setTextColor(Color.parseColor("#333333"));
// Check & Set
if (holder.buttonMinus != null) {
holder.buttonMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.inc >= 0) {
holder.cartQtyTextView.setText(String.valueOf(holder.inc--));
}
}
});
}
holder.buttonMinus.setBackgroundColor(Color.WHITE);
holder.buttonMinus.setTextColor(Color.parseColor("#333333"));
return convertView;
}
支架
class Holder {
ButtonRectangle buttonPlus;
ButtonRectangle buttonMinus;
TextView cartQtyTextView;
TextView textViewItemName;
TextView textViewPrice;
ImageView imageView;
int inc;
}
但这是随机递增的.我该如何解决这个问题?
But this is incrementing randomly. How may I fix this?
推荐答案
> But this is incrementing randomly
这是因为查看者可能属于不同的订单行.
that is because the viewholder may belong to different order-lines.
示例
- 一开始,viewholder#1 属于订单号#1,inc=2
- 如果您向下滚动列表框订单行 #1 将变得不可见而订单行 #27 变得可见..
- 现在订单行 #27 正在重用 viewholder#1 和 inc=2
如何解决这个问题:
您需要一个对象模型来存储数据 inc、articleid、数量、单价、articlename ......
you need an objectmodell that store the data inc, articleid, quantity, unitprice, articlename, ......
final OrderItem orderItem = (OrderItem) objects.get(position);
...
orderItem.inc++;
另见https://stackoverflow.com/search?q=listview+random
这篇关于如何在android中增加视图持有者变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文