在2.3.6和4.2 android版本中,isChecked()行为完全相反

我正在开发一个简单的Android应用程序创build一个ListView与checkbox,如下所示

listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,results); this.setListAdapter(listAdapter); ListView lv = getListView(); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lv.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { CheckedTextView check = (CheckedTextView)view; //The below code needed for running on AVD version 4.2 if(check.isChecked()) { // Checkbox checked do something } else { // Not checked. Do something } /* Exact opposite need on Samsung phone running 2.3.6 if(!check.isChecked()) { // Checkbox checked do something } else { // Not checked. DO something } */ 

这确实很奇怪。 当它们需要完全相反的检查时,应用程序如何兼容这两个版本?

请让我知道如何处理这个问题。 任何帮助将不胜感激。

Solutions Collecting From Web of "在2.3.6和4.2 android版本中,isChecked()行为完全相反"

我知道这个问题,在更新的框架onItemClick调用checkbox的checkd属性之前更改!

在点击项目后(例如closures活动或保存button点击),我检查checkbox的选中状态。

看看list.getCheckedItemPositions() …它将返回一个你可以使用的SparseBooleanArray

编辑:例如

 public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List<Integer> selectedIds = new ArrayList<Integer>(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.entry_data_multi_choice_filter); //initialize the list list = (ListView) findViewById(R.id.list); listAdapter = new MyListAdapterClass(this, R.layout.list_item_multiple_choice); list.setAdapter(listAdapter); //onclick on a item, set the selectaion as invalid list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { savedSelectionInvalid = true; } }); list.setAdapter(listAdapter); //... } //... private void validateChecked() { if(savedSelectionInvalid == true) { SparseBooleanArray checkedItemlist = list.getCheckedItemPositions(); selectedIds.clear(); for (int i=0; i < checkedItemlist.size(); i++){ if (checkedItemlist.valueAt(i)) { selectedIds.add(a.keyAt(i)); } } this.savedSelectionInvalid = false; } } //... //this method se private void saveAndClose() { validateChecked(); //now you have the selectedIds filled with every checked item key! this.finish(); } } 

这个问题可以通过检查设备的版本来解决。

文档build议您Build.VERSION.SDK_INT中的值检查Build.VERSION_CODES

示例用法

 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) { // only for android older than gingerbread } 

http://developer.android.com/reference/android/os/Build.VERSION.html

(更像是一个解决scheme虽然)

你也可以尝试使用这样的东西:

 mFilterListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ... SparseBooleanArray checkedItemlist = mFilterListView.getCheckedItemPositions(); boolean checked = false; for (int i = 0; i < checkedItemlist.size(); i++) { if (checkedItemlist.keyAt(i) == position) { checked = checkedItemlist.valueAt(i); } ... } } 

结果你有一个标志被选中 ,表示点击后项目的状态是什么。