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

ⅰ亾dé卋堺 提交于 2019-12-28 19:25:53

问题


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"Th position .when i scroll down the list-view the values in "0"Th position text view changed to what ever in initial state.

My base adapter class

public class sample extends BaseAdapter{

public ArrayList<HashMap<String, String>> list;
Activity activity;

public sample(Activity activity,ArrayList<HashMap<String, String>> list) {
    super();
    this.activity = activity;
    this.list = list;
}

public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

class ViewHolder {

    Button order;
    TextView item_name, order_qty;
    EditText et_quantity;

}

public View getView(final int position, View convertView,final ViewGroup parent) {
    // TODO Auto-generated method stub



    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<String, String> map = list.get(position);

    holder.item_name.setText(map.get("name"));

    //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(""+holder.order_qty.getText().toString());

        qty++;

        holder.order_qty.setText(""+String.valueOf(qty));
 }
});


return convertView;
}

}

i don't know why the text values changed to initial state when scroll down the customized list view.Can any one know please help me to solve this issue


回答1:


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<String, String> 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;
}



回答2:


I faced the same issue. The reason of the issue is described in the previous answer, however the suggested code block did not help resolving the problem in my case.

I added 350 records to my list view and one adapter object has four EditTexts.

Unexpected behaviors were occurring with TextWatcher listener always.

I used focus check listener to solve this problem:

holder.priceView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus)
                    {
                       // saving data on focus loss.
                       p.setPrice(holder.priceView.getText().toString());
                    }
                }
            });

// holder is my ViewHolder object , priceView is a editText in holder object, p is a reference of my data model.

This works, but the real time updates can be not as expected - like text watchers.

And there is the worst case, when firing last action (without focus losing, last edited value is not triggered and updated).



来源:https://stackoverflow.com/questions/18780180/text-values-are-changed-in-customized-listview-when-scroll-the-listview-in-andro

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