Skip to content Skip to sidebar Skip to footer

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(new View.OnClickListener() {
            @Override
            public void onClick(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, new Integer(quantity));
ibAdd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TextView etQuantityView = (TextView) v.getTag(R.id.TAG_ID_1);
        int prevQuantity = (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>
    <item name="TAG_ID_1" type="id"/>
    <item name="TAG_ID_2" type="id"/>
</resources>

Post a Comment for "Android Custom ListView CursorAdapter Updating The Last Item"