Developer
Developer

Reputation: 1435

Android: simple_list_item_single_choice is not working with ArrayAdapter

In my ListView i want single selection to delete the item. For that i am using simple_list_item_single_choice with ArrayAdapter. It show me the single choice option in my ListView. But i am not able to click on that checkbox.

Here is my code:

       ArrayList array_list_title = mydb.getTitle();
    System.out.println(array_list_title);

    ArrayAdapter<String> arrayAdapter =      
              new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, array_list_title);
    listView.setAdapter(arrayAdapter);

Upvotes: 6

Views: 6031

Answers (1)

Pankaj Kumar
Pankaj Kumar

Reputation: 82968

You need to use ListView.setChoiceMode(int mode). Like

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // Enables multiple selection

or

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // Enables single selection

to enable checkboxes.


So your code will be like

ArrayAdapter<String> arrayAdapter =      
              new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, array_list_title);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setAdapter(arrayAdapter);

Upvotes: 13

Related Questions