我有listview,这是多选模式
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);
这包含多个选项列表项 我想检查是否选中了所选项目 所以我该怎么做。
答案 0 :(得分:2)
您需要抓取已点击的项目,然后遍历它们以查找已检查的项目,如下所示:
// 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()获取用户已检查的项目,无论是否选中该复选框。
答案 1 :(得分:2)
单击时使用了给定列表项的isChecked属性:
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView item = (CheckedTextView)v;
if(item.isChecked()){
//do what you want
}