列表视图项的复选框点击不可预知的行为

时间:2015-07-15 07:05:04

标签: android listview checkbox android-listview getview

在列表视图中,我使用checkboxes选择列表列表项,当检查checkbox时,listview项的背景应该更改,但是当我点击{{1第一个项目的背景更改但是当我向下滚动列表项目3的背景和checkbox状态更改时。我不知道为什么这种情况可能是由于listview的回收过程造成的。

checkbox

我在适配器的public View getView(final int position, View convertView, ViewGroup parent) { TextView tvTitle, tvCat, tvDate; CheckBox cbAddSummary, cbAddPhoto,cbItemSelect; Log.i("mponeList:position",String.valueOf(position)); if (convertView == null) { LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_mp_one,parent,false); tvTitle = (TextView)convertView.findViewById(R.id.tv_title); tvCat = (TextView) convertView.findViewById(R.id.tv_cat); tvDate = (TextView)convertView.findViewById(R.id.tv_date); cbAddSummary = (CheckBox) convertView.findViewById(R.id.cb_add_summary); cbAddPhoto = (CheckBox) convertView.findViewById(R.id.cb_add_photo); cbItemSelect = (CheckBox) convertView.findViewById(R.id.cb_item_select); } tvTitle = (TextView)convertView.findViewById(R.id.tv_title); tvCat = (TextView) convertView.findViewById(R.id.tv_cat); tvDate = (TextView)convertView.findViewById(R.id.tv_date); cbAddSummary = (CheckBox) convertView.findViewById(R.id.cb_add_summary); cbAddPhoto = (CheckBox) convertView.findViewById(R.id.cb_add_photo); cbItemSelect = (CheckBox) convertView.findViewById(R.id.cb_item_select); cbItemSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { View row = ((View) buttonView.getParent()); if (isChecked) { row.setBackgroundResource(R.drawable.shape_mp_list_item_check); } else { row.setBackgroundResource(R.drawable.shape_mm_er_list); } } }); MpOneListModel model = this.list.get(position); tvTitle.setText(model.getTitle()); tvCat.setText(model.getCat()); tvDate.setText(model.getDate()); return convertView; } 中使用的代码。

5 个答案:

答案 0 :(得分:0)

尝试在适配器中添加这两种方法

        @Override
        public int getViewTypeCount() {
            return list.size() + 1;
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

答案 1 :(得分:0)

您应该在模型中保持该检查状态(MpOneListModel)。更新该模型onCheckedChanged

cbItemSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    View row = ((View) buttonView.getParent());
                    if (isChecked) {
                         model.setChecked(true)
                    } else {
                         model.setChecked(false)
                    }
                }
        });

然后添加你的逻辑

if(model.isChecked()) {
    //update background
} else {
    //reverse background
}

目前向下滚动时。 根据列表视图回收再次使用您之前的观看次数。所以你最好把自己的状态保持在模特本身

答案 2 :(得分:0)

在模型中添加isChecked布尔属性并使用false初始化它并使用以下代码:

public View getView(final int position, View convertView, ViewGroup parent)     {
    TextView tvTitle, tvCat, tvDate;
    CheckBox cbAddSummary, cbAddPhoto,cbItemSelect;
    Log.i("mponeList:position",String.valueOf(position));
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_mp_one,parent,false);
        tvTitle = (TextView)convertView.findViewById(R.id.tv_title);
        tvCat = (TextView) convertView.findViewById(R.id.tv_cat);
        tvDate = (TextView)convertView.findViewById(R.id.tv_date);
        cbAddSummary = (CheckBox) convertView.findViewById(R.id.cb_add_summary);
        cbAddPhoto = (CheckBox) convertView.findViewById(R.id.cb_add_photo);
        cbItemSelect = (CheckBox) convertView.findViewById(R.id.cb_item_select);
    }

    tvTitle = (TextView)convertView.findViewById(R.id.tv_title);
    tvCat = (TextView) convertView.findViewById(R.id.tv_cat);
    tvDate = (TextView)convertView.findViewById(R.id.tv_date);
    cbAddSummary = (CheckBox) convertView.findViewById(R.id.cb_add_summary);
    cbAddPhoto = (CheckBox) convertView.findViewById(R.id.cb_add_photo);
    cbItemSelect = (CheckBox) convertView.findViewById(R.id.cb_item_select);

  final MpOneListModel model = this.list.get(position);
    tvTitle.setText(model.getTitle());
    tvCat.setText(model.getCat());
    tvDate.setText(model.getDate());


    cbItemSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                View row = ((View) buttonView.getParent());
                model.isChecked = isChecked;
                if (isChecked) {
                    row.setBackgroundResource(R.drawable.shape_mp_list_item_check);
                } else {
                    row.setBackgroundResource(R.drawable.shape_mm_er_list);
                }
            }
    });



    if(model.isChecked){
        cbItemSelect.setCheck(true);

    }
    return convertView;
}

答案 3 :(得分:0)

不要忘记列表视图重用您的视图。在getView方法中,您应该检查当前项目是否已选中。其中一个变体 - 在适配器中,您可以添加ArrayList并添加已检查的元素。这是示例

MyAdapter extends BaseAdapter {
private List<Model> list;
private ArrayList<> selectedList;

public MyAdapter(List<Model> items) {
this.list = items;
selectedList = new ArrayList(items.size);
}

getView(...) {
...init view here
final Model item = list.get(position);
checkBox.setChecked(selectedList.contains(item));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) selectedList.add(item);
else selectedList.remove(item);
}
});

答案 4 :(得分:0)

您需要在onCheckedChanged()方法中维护列表项的位置。也使用ViewHolder模式。请参阅this。它涵盖了两者。