Android ListView CHOICE_MODE_MULTIPLE, how to set checked index?

旧城冷巷雨未停 提交于 2019-12-18 12:13:08

问题


I'm using the cool feature of the ListView to show a checkbox next to the item in the ListView. I bind my list to an array of strings. The onClick and onSelectedItem listeners get called fine, in this way I know the index of the "string" checked (or unchecked).

I'm storing all the checked strings into preferences (as a comma-concatenated-string), and everytime the activity becomes visible I would like to set the checked items back in the listview.

Is there a way of doing it? or the CHOICE_MODE_MULTIPLE doesn't allow to set the checked items?

note: I'm not using a custom view, since what I want to display is just a string and a checkbox. I've tried setSelection(index) but it should set the onlyone selected (highlighted) row.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,names);
m_playlists_list.setAdapter(adapter);
m_playlists_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

回答1:


Use the setItemChecked method of ListView

Sets the checked state of the specified position. The result is only valid if the choice mode has been set to CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE.



来源:https://stackoverflow.com/questions/5146182/android-listview-choice-mode-multiple-how-to-set-checked-index

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