在android中滚动列表视图时,自定义列表视图中的文本值会发生变化吗? [英] Text values are changed in customized listview when scroll the listview in android?

查看:13
本文介绍了在android中滚动列表视图时,自定义列表视图中的文本值会发生变化吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用带有文本视图、编辑文本和按钮的自定义列表视图.当我单击0"位置的按钮并更改0"位置的文本视图值时.当我滚动时向下列表查看0"位置文本视图中的值更改为初始状态.

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.

我的基本适配器类

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

推荐答案

问题是您没有为每一行使用不同的视图持有者,并且在回收您的列表项视图时,您的持有者被混在一起.

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.

您可以看到这只是向您的持有人添加一个 final int position 字段,其中存储 getView 位置参数;调试,你会看到会发生什么.

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.

因此,您不能使用持有人来存储您的数量值

此外,您没有使用变量 list 来存储数量值.此代码对我有用:

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

这篇关于在android中滚动列表视图时,自定义列表视图中的文本值会发生变化吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆