我爲此創建了一個GridView和一個適配器。在getView()方法中,我做了這個:適配器getView()裏面有監聽器
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder = new RecordHolder();
final Sticker sticker = data.get(position);
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
viewHolder.count = sticker.getAmount();
viewHolder.textViewNumber = (TextView) convertView.findViewById(R.id.textViewNumber);
viewHolder.textViewQuantity = (TextView) convertView.findViewById(R.id.textViewQuantity);
viewHolder.iv_logo_card = (ImageView) convertView.findViewById(R.id.iv_logo_card);
viewHolder.buttonAdd = (ImageButton) convertView.findViewById(R.id.buttonAdd);
viewHolder.buttonRemove = (ImageButton) convertView.findViewById(R.id.buttonRemove);
viewHolder.textViewQuantity.setText(sticker.getAmount().toString());
paintQuantity(viewHolder.count, viewHolder, context);
convertView.setTag(viewHolder);
} else {
viewHolder = (RecordHolder) convertView.getTag();
}
viewHolder.buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolder.count != 99) {
viewHolder.count++;
}
sticker.setAmount(viewHolder.count);
StickerBusinessService.saveSticker(sticker);
viewHolder.textViewQuantity.setText(sticker.getAmount().toString());
paintQuantity(viewHolder.count, viewHolder, context);
}
});
在這個方法中寫入.setOnClickListener是正確的嗎?有更好的方法來做到這一點?
感謝您的回答! 但它不能正常工作......現在,它增加/減少計數,但它不是每個項目的增加/減少,它是所有itens的加/減。你有什麼主意嗎? –
不太確定所有項目的加/減是什麼意思,但是每次調用getView時都需要更新viewHolder.count(假設它是模型的一部分) – fractalwrench
感謝您的所有答案!現在它工作正常! 我只是把下面的代碼當行== 0時: if(row == null){holder} = new RecordHolder(); LayoutInflater inflater =((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId,parent,false); } –