Android Custom Listview Cursoradapter Updating The Last Item
I'm having issue with my custom listview (CursorAdapter) When I click btAdd on 2nd item, it's updating the last list item. And also, when I click an item, it doesn't display this
Solution 1:
ibAdd
Button click always update last row etQuantity
TextView because etQuantity
is reference to TextView which is return by getView
method on last call.
To update clicked row TextView value use setTag/getTag
method to get clicked row in onClick
of ibAdd
and ibSubtract
Buttons. like:
ibAdd.setTag(etQuantity);
ibAdd.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
TextView etQuantityView=(TextView)v.getTag();
quantity++;
etQuantityView.setText(String.valueOf(quantity));
}
});
Do same on ibSubtract
Button click to show subtracted value
Solution 2:
This is improving on ρяσѕρєя K's answer (which had worked for me). This is a better way of using two tags:
ibAdd.setTag(R.id.TAG_ID_1, etQuantity);
ibAdd.setTag(R.id.TAG_ID_2, newInteger(quantity));
ibAdd.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
TextViewetQuantityView= (TextView) v.getTag(R.id.TAG_ID_1);
intprevQuantity= (Integer) v.getTag(R.id.TAG_ID_2);
prevQuantity++;
etQuantityView.setText(String.valueOf(prevQuantity));
}
});
NOTE: The ids R.id.TAG_ID_1 and R.id.TAG_ID_2 are required to be defined in the res/values folder as tags.xml. The contents are like this:
<resources><itemname="TAG_ID_1"type="id"/><itemname="TAG_ID_2"type="id"/></resources>
Post a Comment for "Android Custom Listview Cursoradapter Updating The Last Item"