Text values are changed in customized listview when scroll the listview in android?

前端 未结 2 1248
礼貌的吻别
礼貌的吻别 2020-12-10 23:05

In my application am using customized list-view with text view,edit-text and buttons.When i click the button in \"0\"Th position and am change the text view values in \"0\"T

2条回答
  •  春和景丽
    2020-12-10 23:38

    The problem is that you are not using a different view holder for every line, and that when recycling your list item views your holders are shuffled togheter.

    You can see this simply adding a final int position field to your holder, where to store the getView position parameter; debugging, you will see what happen.

    So, you can't use holders to store your quantity values

    Furthermore, you are not using the variable list to store quantity values. This code is working for me:

        public View getView(final int position, View convertView,final ViewGroup parent) {
    
        final ViewHolder holder;
        LayoutInflater inflater = activity.getLayoutInflater();
    
        if (convertView == null) {
    
            convertView = inflater.inflate(R.layout.order_custom, null);
    
            holder = new ViewHolder();
    
            holder.order = (Button) convertView.findViewById(R.id.order);
            holder.item_name = (TextView) convertView.findViewById(R.id.item_name);
            holder.order_qty = (TextView) convertView.findViewById(R.id.order_count);
    
            convertView.setTag(holder);
    
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        try{
            HashMap map = list.get(position);
    
            holder.item_name.setText(map.get("name"));
            holder.order_qty.setText(map.get("quantity"));
    
            //Log.v("Available or not", ""+map.get("available"));
    
    
        }catch(Exception e){
            e.printStackTrace();
        }
    
    
        holder.order.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
    
                //here am click the button to change order count text value
                int qty = Integer.valueOf(list.get(position).get("quantity"));
    
                qty++;
    
                holder.order_qty.setText(""+String.valueOf(qty));
                list.get(position).put("quantity", qty+"");
            }
        });
    
    
        return convertView;
    }
    

提交回复
热议问题