Selecting multiple items in ListView

2019-01-02 23:24发布

How to select multiple item in ListView in android.

标签: android
10条回答
小情绪 Triste *
2楼-- · 2019-01-02 23:28

You cannot "select" multiple items in a ListView. You can use CHOICE_MODE_MULTIPLE and check multiple items in a ListView.

查看更多
Anthone
3楼-- · 2019-01-02 23:28

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

查看更多
淡お忘
4楼-- · 2019-01-02 23:31

Step 1: setAdapter to your listview.

 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.

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.

@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();

    }
查看更多
ら.Afraid
5楼-- · 2019-01-02 23:34

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

link

developer.android

查看更多
在下西门庆
6楼-- · 2019-01-02 23:36

and to get it :

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

                Log.d(getLocalClassName(), "onItemClick(" + view + ","
                        + position + "," + id + ")");
        }
    });
查看更多
forever°为你锁心
7楼-- · 2019-01-02 23:38

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());**

               }
            });
查看更多
登录 后发表回答