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

落花浮王杯 提交于 2019-12-06 08:49:27

问题


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.


回答1:


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;
         }
       }



回答2:


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>



回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!