Reputation: 313
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
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
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
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