listView复选框滚动错误

时间:2016-05-06 11:11:23

标签: android listview android-checkbox

我遇到了包含复选框的listView的问题。当我滚动一些项目时会自动选中。 这是我的自定义适配器中的getView方法的代码:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.prerequisite_course, parent, false);

        holder = new ViewHolder();
        holder.name = (CheckBox) convertView.findViewById(R.id.course_checkbox);
        convertView.setTag(holder);

        holder.name.setOnClickListener( new View.OnClickListener() {
            public void onClick(View v) {
                CheckBox cb = (CheckBox) v ;
                Course course = (Course) cb.getTag();
                if (cb.isChecked())
                    checkBoxChecked(cb, course);
                else
                    checkBoxNotChecked(cb, course);
            }
        });
    }

    else {
        holder = (ViewHolder) convertView.getTag();
    }

    Course course = coursesDataManager.getCourseInPosition(position);
    String currentId = COURSES_SUBJECT + " " + course.getId();
    holder.name.setText(currentId);
    holder.name.setTag(course);

    // for the clear button:
    if (!holder.name.isChecked()) {
        checkBoxNotChecked(holder.name, (Course) holder.name.getTag());
    }

    return convertView;
}

private void checkBoxChecked(CheckBox cb, Course add) {
    cb.setBackgroundColor(Color.rgb(1, 67, 121));
    cb.setTextColor(Color.WHITE);
    myCoursesManager.addPrerequisite(add);
}

private void checkBoxNotChecked(CheckBox cb, Course remove) {
    cb.setBackgroundColor(Color.WHITE);
    cb.setTextColor(Color.BLACK);
    myCoursesManager.removePrerequisite(remove);
}

此外,对于代码的批评将受到欢迎,因为我是Android开发的新手。谢谢。

2 个答案:

答案 0 :(得分:2)

你需要为复选框维护布尔数组,试试这种方式对我有用

public class CustomAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private final Context context;
    private List<ModelPooja> listData;

    public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
            holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
            holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checks.setOnCheckedChangeListener(null);
        holder.checks.setFocusable(false);

        if (listData.get(position).isselected) {
            holder.checks.setChecked(true);
        } else {
            holder.checks.setChecked(false);
        }

        holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton cb, boolean b) {

                if (checkMaxLimit()) {

                    if (listData.get(position).isselected && b) {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;

                    } else {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;
                        Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (b) {
                        listData.get(position).isselected = true;
                    } else {
                        listData.get(position).isselected = false;
                    }
                }
            }
        });

        holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
        return convertView;
    }

    public boolean checkMaxLimit() {
        int countermax = 0;
        for(ModelPooja item : listData){
            if(item.isselected){
                countermax++;
            }
        }
        return countermax >= 5;
    }

    public class ViewHolder {
        TextView tv;
        public CheckBox checks;
    }
}

答案 1 :(得分:0)

创建一个Model Class,用于保存列表项的checkBox状态。

public class ModelClass
{
    int position=null;
    String item=null;
    boolean selected=false;
    public ModelClass(int position,String item, boolean selected) {
    super();
    this.position=position;
    this.item=item;
    this.selected = selected;
    }
}

然后在适配器类

private ArrayList<ModelClass> arl;

在getView()

 holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                arl.get(position).selected=isChecked;
            }
        });


 holder.checkBox.setChecked(arl.get(position).selected);

现在无论你滚动多少你的checkBoxes状态都是一致的(相信我,我在我的项目中使用了这个代码)。