Why is ListView.getCheckedItemPositions() not returning correct values?

前端 未结 15 1966
北海茫月
北海茫月 2020-11-29 06:01

The app has a ListView with multiple-selection enabled, in the UI it works as expected. But when I read the values out using this code:

Log.         


        
15条回答
  •  心在旅途
    2020-11-29 06:17

    This adjustment fixed it for me. Change the getView method so that "int position" is "final int position". Then do this with your checkbox:

    ((CheckBox) convertView).setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        list.setItemChecked(position, ((CheckBox) buttonView).isChecked());
                    }
                });
    

    Let me elaborate. list is a reference to the listview and in my case the adapter is an inner class in the dialog holding the list.

提交回复
热议问题