Android通过Adapter获取ListView中的CheckBox多选内容


  • 前言:获取ListViewCheckBox多选值的方式多种多样,比如利用setMultiChoiceModeListener这个接口通过监听ListView获取到选中值,也可以通过HashMap的键值来记录CheckBox对应位置的选中状态从而拿到选中值,不过我自己结合网上的情况做了一个比较简单的方法,本人是个小白,也不知道这种方法的的利弊,只是想分享出来,大神如果有什么看法请指导,谢谢;

  • 原理:在自定义Adapter中创建存储CheckBox值的数组checkBoxIDList,通过设置CheckBox的状态改变监听事(setOnCheckedChangeListener)checkBoxIDList进行值的添加与移除;在Adapter中对checkBoxIDList进行get set,最后在MainActivity即可通过get拿到checkBoxIDList中的值(即checkBox选中值);

  • 重点代码:
    Adapter:setOnCheckedChangeListener
    MainActivity:adapter.getCheckBoxIDList()

Adapter;


public class Adapter extends BaseAdapter {

    private List stringList;
    private Context mContext;
    private List checkBoxIDList;            //存储checkBox的值

    //get set
    public List getCheckBoxIDList() {
        return checkBoxIDList;
    }
    public void setCheckBoxIDList(List checkBoxIDList) {
        this.checkBoxIDList = checkBoxIDList;
    }
    public Adapter(List stringList, Context mContext) {
        this.stringList = stringList;
        this.mContext = mContext;
        checkBoxIDList= new ArrayList<>();
    }

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

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

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

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

        final TestViewHolder testViewHolder;
        
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null);
            testViewHolder = new TestViewHolder();

            testViewHolder.item_checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

            convertView.setTag(testViewHolder);
        } else {
            testViewHolder = (TestViewHolder) convertView.getTag();
        }

        //设置checkBox的值
        testViewHolder.item_checkBox.setText("List:" + stringList.get(position).toString());

        //获取复选框选中状态改变事件进行增删改
        testViewHolder.item_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                /*
                * b=选中状态
                * if b = true 将值添加至checkBoxIDList
                * if b = false 将值从checkBoxIDList移除
                * */
                if (b) {

            checkBoxIDList.add(testViewHolder.item_checkBox.getText().toString());

                } else {
                   
            checkBoxIDList.remove(testViewHolder.item_checkBox.getText().toString());
                
                }
            }
        });
        return convertView;
    }

    static class TestViewHolder {
    
        CheckBox item_checkBox;
       
    }

}


MainActivity;

public class MainActivity extends AppCompatActivity {

    private List TestAdapterList;
    private Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ListView listView = (ListView) findViewById(R.id.listview);
        
        //对List赋值
        TestAdapterList= new ArrayList<>();
        TestAdapterList.add("1");
        TestAdapterList.add("2");
        TestAdapterList.add("3");
        TestAdapterList.add("4");
        TestAdapterList.add("5");
        TestAdapterList.add("6");
        TestAdapterList.add("7");
        TestAdapterList.add("8");
        TestAdapterList.add("9");

        //setAdapter
        adapter = new Adapter(TestAdapterList, MainActivity.this);
        listView.setAdapter(adapter);

        //button单击
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                System.out.println("------------");
                /*
                *循环打印选中checkBox的值
                *通过adapter.getCheckBoxIDList()获取所选checkBox值的集合
                */
                for (int i = 0; i < adapter.getCheckBoxIDList().size(); i++) {

                System.out.println(adapter.getCheckBoxIDList().get(i).toString());

                }

            }
        });
    }
}

你可能感兴趣的:(Android通过Adapter获取ListView中的CheckBox多选内容)