Android 在 ListView 中选择多个项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1362602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:00:01  来源:igfitidea点击:

Selecting multiple items in ListView

android

提问by bhatt4982

How to select multiple item in ListView in android.

如何在android中的ListView中选择多个项目。

回答by bhatt4982

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

其实你可以 ;) 这只是用户体验的问题,对吧?

Try this, (1) for list control set

试试这个,(1)用于列表控件集

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

(2) define list item as

(2) 定义列表项为

<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_choiceexcept android:background="@drawable/txt_view_bg

这与android.R.layout.simple_list_item_multiple_choice除了 android:background="@drawable/txt_view_bg

(3) And define drawable txt_view_bg.xml as

(3) 并将 drawable txt_view_bg.xml 定义为

<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.

注意:- 处理多项选择的首选方法是通过单击项目单击来跟踪自己的选择,而不是根据其在列表中的状态。

回答by Ramesh Akula

Step 1:setAdapter to your listview.

第 1 步:将适配器设置为您的列表视图。

 listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, GENRES)); 

Step 2:set choice mode for listview .The second line of below code represents which checkbox should be checked.

步骤 2:为 listview 设置选择模式。下面代码的第二行表示应该选中哪个复选框。

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemChecked(2, true);

listView.setOnItemClickListener(this);



 private static  String[] GENRES = new String[] {
        "Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
        "Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
    };

Step 3:Checked views are returned in SparseBooleanArray, so you might use the below code to get key or values.The below sample are simply displayed selected names in a single String.

第 3 步:在 SparseBooleanArray 中返回检查的视图,因此您可以使用下面的代码来获取键或值。下面的示例只是在单个字符串中显示选定的名称。

@Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int arg2, long arg3)
    {

    SparseBooleanArray sp=getListView().getCheckedItemPositions();

    String str="";
    for(int i=0;i<sp.size();i++)
    {
        str+=GENRES[sp.keyAt(i)]+",";
    }
    Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();

    }

回答by AlexAndro

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

此示例存储您已检查的值并在 Toast 中显示它们。当您取消选中项目时它会更新http://android-coding.blogspot.ro/2011/09/listview-with-multiple-choice.html

回答by Mario Velasco

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

要在取消选中某些项目后“更新” Toast 消息,只需将此行放在 for 循环中:

if (sp.valueAt(i))

so it results:

所以结果:

for(int i=0;i<sp.size();i++)
{
    if (sp.valueAt(i))
        str+=names[sp.keyAt(i)]+",";
}

回答by Girish Kumar Shakya

It's very simple,

很简单,

listViewRequests.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    **AppCompatCheckedTextView checkBox = (AppCompatCheckedTextView) view;**
                    Log.i("CHECK",checkBox.isChecked()+""+checkBox.getText().toString());**

               }
            });

回答by Sam

I would advice to check the logic of ListActivityaccording to what is needed could be the best way not to lose much time

我建议ListActivity根据需要检查逻辑可能是不浪费太多时间的最佳方法

link

关联

developer.android

开发者.android

回答by H.Fa8

In listView you can use it by Adapter

在 listView 中,您可以通过 Adapter 使用它

ArrayAdapter<String> adapterChannels = new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice);

回答by Ramees

Best way is to have a contextual action bar with listview on multiselect, You can make listview as multiselect using the following code

最好的方法是在多选上使用带有列表视图的上下文操作栏,您可以使用以下代码将列表视图设为多选

listview.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);

And now set multichoice listener for Listview ,You can see the complete implementation of multiselect listview at Android multi select listview

并且现在为Listview设置multichoice listener,你可以在Android multi select listview看到multiselect listview的完整实现

回答by Baadsah

You have to select the option in ArrayAdapter:

您必须选择以下选项ArrayAdapter

ArrayAdapter<String> adapter = new ArrayAdapter<String>
    (this, android.R.layout.simple_list_item_single_choice, countries);

回答by zobi8225

and to get it :

并得到它:

public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

                Log.d(getLocalClassName(), "onItemClick(" + view + ","
                        + position + "," + id + ")");
        }
    });