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

一个人想着一个人 提交于 2019-12-04 14:54:29

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.

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