How to increment a value on view holder variables in android?

前端 未结 3 1695
深忆病人
深忆病人 2021-01-22 18:18

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



        
相关标签:
3条回答
  • 2021-01-22 18:55
     > But this is incrementing randomly
    

    that is because the viewholder may belong to different order-lines.

    Example

    • in the beginning viewholder#1 belongs to orderline #1 with inc=2
    • if you scroll down the listbox orderline #1 becomes invisible and orderline #27 becomes visible. .
    • now orderline #27 is reusing viewholder#1 and with inc=2

    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

    0 讨论(0)
  • 2021-01-22 18:59

    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();
        }
    
    0 讨论(0)
  • 2021-01-22 19:04

    When you click button it gets the TextView corresponding to that position..

    1. At OnClick button, get TextView of position you Clicked..

    2. 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..

    0 讨论(0)
提交回复
热议问题