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.
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;
}
}
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>
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.
来源:https://stackoverflow.com/questions/15083422/how-to-switch-listview-choice-mode-from-single-to-multiple-on-clicking-event-in