How to check listitem checked or not in listview android browser?

I have a listview that is a multiple select mode

lView = (ListView) findViewById(R.id.ListView01); lView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, lv_items)); lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

contains several items in the select list I want to check if the selected item is checked or not as I can do this.

+4
source share
2 answers

You need to grab the items that were clicked, and then scroll through them to find the marked ones this way:

 // Using a List to hold the IDs, but could use an array. List<Integer> checkedIDs = new ArrayList<Integer>(); // Get all of the items that have been clicked - either on or off final SparseBooleanArray checkedItems = lView.getCheckedItemPositions(); for (int i = 0; i < checkedItems.size(); i++){ // And this tells us the item status at the above position final boolean isChecked = checkedItems.valueAt(i); if (isChecked){ // This tells us the item position we are looking at final int position = checkedItems.keyAt(i); // Put the value of the id in our list checkedIDs.put(position); } } 

Note getCheckedItemPositions () gets the items that have been checked by the user, regardless of whether the check box is checked or not.

+2
source

I used the isChecked property of this list item when clicked:

 protected void onListItemClick(ListView l, View v, int position, long id) { CheckedTextView item = (CheckedTextView)v; if(item.isChecked()){ //do what you want } 
+2
source

Source: https://habr.com/ru/post/1369472/


All Articles