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

空扰寡人 提交于 2019-12-02 05:15:39

问题


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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        holder = new Holder();
        LayoutInflater inflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.menu_list_item, null);

        holder.textViewItemName = (TextView) convertView.findViewById(R.id.textViewItemName);
        holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
        holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewItem);
        holder.buttonPlus = (ButtonRectangle) convertView.findViewById(R.id.buttonPlus);
        holder.cartQtyTextView = (TextView) convertView.findViewById(R.id.textViewCartQty);
        holder.buttonMinus = (ButtonRectangle) convertView.findViewById(R.id.buttonMinus);
        convertView.setTag(holder);
    } else {
        holder = (Holder) convertView.getTag();
    }
    final MenuItem listItem = objects.get(position);
    holder.textViewItemName.setText(listItem.getItemName());
    holder.textViewPrice.setText("$ ".concat(String.valueOf(listItem.getItemPrice())));

    // Check & Set
    if (holder.buttonPlus != null) {
        holder.buttonPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (holder.cartQtyTextView != null) {
                    if (holder.inc >= 0) {
                        holder.cartQtyTextView.setText(String.valueOf(holder.inc++));
                    }
                }
            }
        });
    }
    holder.buttonPlus.setBackgroundColor(Color.WHITE);
    holder.buttonPlus.setTextColor(Color.parseColor("#333333"));

    // Check & Set
    if (holder.buttonMinus != null) {
        holder.buttonMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.inc >= 0) {
                    holder.cartQtyTextView.setText(String.valueOf(holder.inc--));
                }
            }
        });
    }
    holder.buttonMinus.setBackgroundColor(Color.WHITE);
    holder.buttonMinus.setTextColor(Color.parseColor("#333333"));

    return convertView;

}

Holder

class Holder {
    ButtonRectangle buttonPlus;
    ButtonRectangle buttonMinus;
    TextView cartQtyTextView;
    TextView textViewItemName;
    TextView textViewPrice;
    ImageView imageView;
    int inc;
}

But this is incrementing randomly. How may I fix this?


回答1:


 > 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




回答2:


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




回答3:


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();
    }


来源:https://stackoverflow.com/questions/32497092/how-to-increment-a-value-on-view-holder-variables-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!