I have a viewholder
in my Android app and I tried to set an incremented value on a textfield. I tried it like following,
Activity
> But this is incrementing randomly
that is because the viewholder may belong to different order-lines.
Example
how to fix this:
you need an objectmodell that store the data inc, articleid, quantity, unitprice, articlename, ......
final OrderItem orderItem = (OrderItem) objects.get(position);
...
orderItem.inc++;
See also https://stackoverflow.com/search?q=listview+random
You also need to set a tag
for the Button, if you want to manipulate the holder item.
if (convertView == null) {
...
holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
holder.buttonPlus.setTag(holder);
holder.buttonPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Holder current= (Holder) v.getTag();
current.inc=current.inc+1;
notifyDataSetChanged();
}
});
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
When you click button it gets the TextView corresponding to that position..
At OnClick
button, get TextView of position you Clicked..
getText()
of that TextView, implement condition to increase/decrease your inc
then setText()
to that TextView
I would also suggest to take a local variable inside your Adapter
class instead of Holder
class as it would be easy to manipulate its value..