如何检查listitem被检查或不在listview android?

如何检查listitem被检查或不在listview android?

问题描述:

我有列表视图这是选择题模式如何检查listitem被检查或不在listview android?

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

这包含多种选择的列表项 我要检查选择的项目是否被选中或不 这样我怎么能做到这一点。

你需要抓住已经点击的项目,然后遍历它们找到托运的像这样:

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

注意的getCheckedItemPositions()获取已通过检查的项目用户,无论该复选框是否被选中。

+0

lView.getCheckedItemPositions();给我所有我想要取消选中的选中项目 – swan

+1

如果你知道listView中有多少项目,并且你知道已经检查过的项目的索引,难道不能够简单地找出那些未经检查的项目吗? – SBerg413

我使用的给定列表项的财产器isChecked当点击:

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