我使用的是帶有MULTIPLE_CHOICE
的ListView,並使用setItemChecked()
方法獲取選定的項目。如何取消選中由setItemChecked()檢查的項目?
它工作正常,因爲我能夠看到以前檢查的項目。 的問題是,如果我取消選中的一個先前檢查項目,然後通過custList.getCheckItemIds()
得到檢查的項目列表中的陣列仍然有我未選中的項目。
任何人都可以請告訴我,如果這應該發生或我錯過了什麼?
我使用的是帶有MULTIPLE_CHOICE
的ListView,並使用setItemChecked()
方法獲取選定的項目。如何取消選中由setItemChecked()檢查的項目?
它工作正常,因爲我能夠看到以前檢查的項目。 的問題是,如果我取消選中的一個先前檢查項目,然後通過custList.getCheckItemIds()
得到檢查的項目列表中的陣列仍然有我未選中的項目。
任何人都可以請告訴我,如果這應該發生或我錯過了什麼?
在這裏,您必須調用setOnCheckedChangeListener,並且您必須管理此偵聽器塊內的代碼。
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Write and manage your code here.
}
});
如果你只是試圖找出哪些項目在任何特定時間檢查,你可以從一個的ListView SparseBooleanArray,並用遍歷它的循環。例如:
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++){
if (checked.get(i))
//the item at index i is checked, do something
else
//the item is not checked, do something else
}
這樣的:
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++){
if (checked.get(i))
//the item at index i is checked, do something
else
//the item is not checked, do something else
}
好好嘗試的工作。
遵循Multiple Contact Picker List [getCheckedItemPositions()]
應該沒問題。
SparseBooleanArray selectedPositions = listView.getCheckedItemPositions();
for (int i=0; i<selectedPositions.size(); i++) {
if (selectedPositions.get(selectedPositions.keyAt(i)) == true) {
//do stuff
}
}
我沒有使用自定義佈局,所以我不知道如何才能夠獲得CheckBox對象。這裏是代碼片段: 'custList =(ListView)findViewById(R.id.custList); \t \t \t custList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); //數組適配器填充列表使用Android佈局用於多選的列表 adaptCust = new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice,customers); \t \t custList.setAdapter(adaptCust); ' –
vishpoison