Selecting multiple items in ListView

2019-01-02 23:24发布

How to select multiple item in ListView in android.

标签: android
10条回答
来,给爷笑一个
2楼-- · 2019-01-02 23:39

This example stores the values you have checked and displays them in a toast. And it updates when you uncheck items http://android-coding.blogspot.ro/2011/09/listview-with-multiple-choice.html

查看更多
Ridiculous、
3楼-- · 2019-01-02 23:39

To "update" the Toast message after unchecking some items, just put this line inside the for loop:

if (sp.valueAt(i))

so it results:

for(int i=0;i<sp.size();i++)
{
    if (sp.valueAt(i))
        str+=names[sp.keyAt(i)]+",";
}
查看更多
手持菜刀,她持情操
4楼-- · 2019-01-02 23:45

You have to select the option in ArrayAdapter:

ArrayAdapter<String> adapter = new ArrayAdapter<String>
    (this, android.R.layout.simple_list_item_single_choice, countries);
查看更多
你好瞎i
5楼-- · 2019-01-02 23:49

Actually you can ;) It's just a matter of user experience, right?

Try this, (1) for list control set

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);   
listView.setItemsCanFocus(false);

(2) define list item as

<CheckedTextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:background="@drawable/txt_view_bg" />

This is same as android.R.layout.simple_list_item_multiple_choice except android:background="@drawable/txt_view_bg

(3) And define drawable txt_view_bg.xml as

<item android:drawable="@drawable/selected"
  android:state_checked="true" />  
<item android:drawable="@drawable/not_selected" />

Note:- The preferred way to handle multiple choice is to track choices your-self with on click item click, rather than depending on its state in list.

查看更多
登录 后发表回答