android listview中自定义适配器和复选框

文章标签: android,android-listview
2014-11-4 15:11:15     人阅读    

下面的代码中报错了,代码如下

@Override
p
public View getView(final int position, final View convertView, final ViewGroup parent) {
 
    final ViewHolder viewHolder;
 
    View view = convertView;
 
    if (view == null) {
 
        view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
 
        viewHolder = new ViewHolder();



        viewHolder.textTitle = (TextView) view.findViewById(R.id.title);
 
        viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);



        viewHolder.checkBox.setTag(position);
 
        view.setTag(viewHolder);



        viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
 
    } else {
 
        viewHolder = (ViewHolder) view.getTag();
 
        viewHolder.checkBox.getTag(position);
 
    }



    viewHolder.textTitle.setText(getItem(position).getTitle());



    viewHolder.checkBox.setChecked(myIntegerArrayList.contains(position));
 
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
 
        @Override
 
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 
            if (isChecked) {
 
                myIntegerArrayList.add(position);
 
            } else {
 
                myIntegerArrayList.remove((Object) position);
 
            }
 
        }
 
    });



    return view;
}
}




private static class ViewHolder {
 
    TextView textTitle;
 
    CheckBox checkBox;
}
}


该复选框本身工作正常。位置正确添加到我的ArrayList,但如果(假设我选中的项目在位置1)我把listview滚动到底部,然后再滚回去,选中取消了....

 

处理方法

删掉代码viewHolder.checkBox.setTag(position); 
把 viewHolder.checkBox.getTag();放到  onCheckedChanged 中,你按照下面的代码改动

@Override
p
public View getView(final int position, final View convertView, final ViewGroup parent) {
 
    final ViewHolder viewHolder;
 
    View view = convertView;
 
    if (view == null) {
 
        view = LayoutInflater.from(mContext).inflate(R.layout.row, parent, false);
 
        viewHolder = new ViewHolder();



        viewHolder.textTitle = (TextView) view.findViewById(R.id.title);
 
        viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);




        view.setTag(viewHolder);



        viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
 
    } else {
 
        viewHolder = (ViewHolder) view.getTag();
 
        viewHolder.checkBox.getTag(position);
 
    }
 
    viewHolder.checkBox.setTag(position);



    viewHolder.textTitle.setText(getItem(position).getTitle());



    viewHolder.checkBox.setChecked(myIntegerArrayList.contains(position));
 
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
 
        @Override
 
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 
            if (isChecked) {
 
                myIntegerArrayList.add(viewHolder.checkBox.getTag());
 
            } else {
 
                myIntegerArrayList.remove((Object) viewHolder.checkBox.getTag());
 
            }
 
        }
 
    });



    return view;
}
}

让你删掉 viewHolder.checkBox.setTag(position); 是为了 回收ListView,你需要报错位置在tag中,并让 onCheckedChanged 获得到
添加viewHolder.checkBox.getTag() 也是同样的原因


原文地址:http://www.itmmd.com/201411/119.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

在android 自定义listView中绘制矩形   上一篇
下一篇  android 重新加载listview数据
精彩回复
我要追加问题,请求站长解决!
姓名:       

《程序员app》专门为程序员量身定做!