Getting all checked items from listview Android

依然范特西╮ 提交于 2019-12-08 08:40:30

You can maintain a boolean array of all the selected checkbox-

  1. Declare Boolean array in your adapter - boolean[] checkBoxState
  2. Initialize it in your adapter's constructor - checkBoxState= new boolean[list.size()]
  3. Then use this array in your getView method -

     holder.checkBox.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v)
     {
                    if(((CheckBox)v).isChecked())
                    {                   
                         checkBoxState[position]=true;
                        }       
                        else
                        {
                            checkBoxState[position]=false;
    
                        }
                       }
                       });
    
  4. Retrieve the position from this array (Here adapter is the object of your custom adapter) -

    for(int k=0;k< adapter.checkBoxState.length ;k++) {

    if(adapter.checkBoxState[k]==true) { }`

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!