ListView+CheckBox 选中状态错乱问题

最近在开发一个功能,需要选中ListView的item第一次使用这个组合,遇到一个问题 如图:
这里写图片描述
虽然能返回实际选中的值 但是下面的部分item也会被选中
状态错乱!
代码:
第二个activity

public class Main2Activity extends AppCompatActivity {

    private ListView listView;
    private Button btn;
    private List<Map<String, String>> listdata = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        btn = (Button) findViewById(R.id.btn1);
        listView = (ListView) findViewById(R.id.listview);
        initdata();
        final ListAdapter adapter = new ListAdapter(this, listdata);
        listView.setAdapter(adapter);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Info.value = "";
                Iterator<Integer> iterator = adapter.isSelected.keySet().iterator();
                while (iterator.hasNext()) {
                    Integer next = iterator.next();
                    Boolean able = adapter.isSelected.get(next);
                    if (able) {
                        Info.value += listdata.get(next).get("value");
                    }
                }
                finish();
            }
        });
    }

    private void initdata() {
        listdata = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("value", "value" + i);
            listdata.add(map);
        }
    }
}

adapter

public class ListAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<Map<String, String>> listdata ;
    public  HashMap<Integer, Boolean> isSelected;
    private ViewHolder holder;

    public ListAdapter(Context context,List<Map<String, String>> listdata){
        super();
        this.context = context;
        this.listdata = listdata;
        init_1();
    }

    private void init_1() {
        if (isSelected == null) {
            isSelected = new HashMap<Integer, Boolean>();
            for (int i = 0; i < listdata.size(); i++) {
                isSelected.put(i, false);
            }
        }
    }

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

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

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

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

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
            holder.name = (TextView) convertView.findViewById(R.id.tv_1);
            holder.check_state = (CheckBox) convertView.findViewById(R.id.check_state);
            // 将holder绑定到convertView
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(listdata.get(position).get("value"));
        holder.check_state.setTag(position);// setTag的对象是position,以便下面的onClick使用
        holder.check_state.setOnClickListener(this);

        return convertView;
    }

    @Override
    public void onClick(View v) {

        Integer tag = (Integer) v.getTag();
        if (isSelected.get(tag)) {// 先判断isSelected中是否已经选中
            // 选中就置为false,即不选中
            isSelected.put(tag, false);
        } else {
            // 选中
            isSelected.put(tag, true);
        }
        notifyDataSetChanged();

    }

    class ViewHolder {
        private TextView name;
        private CheckBox check_state;
    }
}

Info.java

public class Info {
    public static Map<Integer, Boolean> isSelect = null;
    public static String value = "";
}

状态错乱了,既然状态错乱,那就自己创建一个集合保存checkBox的状态 其实集合在上述代码中已经创建了,对 就是isSelected,通过这个集合里面的值来判断checkbox的状态就好了,物尽其用;

holder.check_state.setChecked(isSelected.get(position));

在上面的adapter里面加入这一行代码,完美决绝
这里写图片描述

推荐大家也看一下这篇博客:错乱的原因解释的很到位,我就不多说了
Android ListView CheckBox状态错乱

相关文章
相关标签/搜索