我在我的Android應用程序中有一個viewholder
,我嘗試在文本字段上設置遞增值。我試圖像下面,如何在android中增加視圖持有者變量的值?
活動
@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;
}
但這是隨機遞增。我該如何解決這個問題?
你是否正在檢查'int'的值與你點擊的位置.. – Mohit
nope。我怎樣才能做到這一點? – codebot
爲什麼你在持有人中創建該公司在活動中聲明 –