Gaurav Agrawal
Gaurav Agrawal

Reputation: 313

How to switch ListView choice mode from single to multiple on Clicking event in android?

i want to know how can i switch list view choice mode from single to multiple on button click. so that i can select multiple list item and delete it and after deleting i should back to single choice mode. if you have any idea how to implement this help me. Thanks.

Upvotes: 3

Views: 5993

Answers (3)

Kapil Vats
Kapil Vats

Reputation: 5515

Calling setChoiceMode is not enough to display checkboxes beside your list rows. If you are using a basic layout for the rows, try android.R.layout.simple_list_item_multiple_choice. Else, you will have to add a checkbox to your row layout & manage its on/off state yourself in the adapter's getView method.

Upvotes: 2

Rohit
Rohit

Reputation: 490

you can use the following code for that:

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:choiceMode="multipleChoice"
     >
</ListView>

Upvotes: 2

Avadhani Y
Avadhani Y

Reputation: 7636

Implement OnClick functionality of the button and check the ListView's status mode and change based on your preference as below....

   public void onClick(View v) {

   switch(v.getId()){
      case (R.id.mybutton):       
         ListView listView = getListView();
           if (listView.getChoiceMode() == ListView.CHOICE_MODE_MULTIPLE)
            {
               listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            }
            else if (listView.getChoiceMode() == ListView.CHOICE_MODE_SINGLE)
             {
              listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
             }
            break;
         }
       }

Upvotes: 2

Related Questions