Select multiple selected / checked / activated in ListView

Wow, the documentation is terrible when it comes to choosing a list item. All I need to do is select and select multiple items in the list. I browsed the web and saw android:choiceMode="multipleChoice" links android:choiceMode="multipleChoice" , which I assume allows you to select multiple items. But where can I get the selected items in my activity? And how did it happen when I try to select multiple elements using setSelection(position) , the previously selected element disappears?

Google also describes in View.setActivated(boolean) that

Please note that activation does not match the selection. Selection is a transient property that represents the view (hierarchy) that the user interacts with. Activation is a long-term condition in which a user can move views to and from. For example, in list mode with one or more parameters enabled, views in the current selection set are activated. (Hmm, yes, we are very sorry for the terminology here.)

So should I use activation instead of choosing? This answer "Answer" talks about how "activated" only the version is "verified" after HoneyComb. But if you must use "activated" for multiple choice, what is the point of android:choiceMode="multipleChoice" ?

+7
java android
source share
1 answer

Therefore, apparently, I was fired from the wrong path, because I had to look for the selected "checked" items, and not the "selected" items. So many answers told me to use selector with my ListView layout using android:listSelector="@drawable/myselector , but I really needed to use selector with my row layout. The solution is actually pretty simple, I'll post it below:

range hood /rowbackgroundselector.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_activated="true" android:drawable="@android:color/holo_green_light"/> </selector> 
  • note how you use "state_activated" to detect if the item is "checked" ...

range hood /mylistrow.xml

 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:background="@drawable/rowbackgroundselector" android:padding="10sp" /> 
  • using selector for string background

MainActivity.onListItemClick ()

 public void onListItemClick(ListView l, View v, int position, long id) { getListView().setItemChecked(pos, true); } 

Finally, make sure your adapter uses your own string layout

 mAdapter = new ArrayAdapter<FileTag>(this.getActivity(), R.layout.mylistrow, mList); 
+13
source share

All Articles