开发者

how to check listitem is checked or not in listview android?

开发者 https://www.devze.com 2023-04-01 00:18 出处:网络
i have listview which is multiple choice mode lView = (ListView) findViewById(R.id.ListView01); lView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.s开发者_如何转开发imple_list_item

i have listview which is multiple choice mode

lView = (ListView) findViewById(R.id.ListView01);
lView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.s开发者_如何转开发imple_list_item_multiple_choice, lv_items));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

this contains multiple choice list items i want to check whether selected item is checked or not so how i can do that.


You need to grab the items that have been clicked and then iterate through them to find the checked ones like so:

// 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 the getCheckedItemPositions() gets the items that have been checked by the user regardless if the checkbox was left checked or not.


I used the isChecked property of the given 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
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消