isChecked()行为与2.3.6和4.2安卓版本完全相反

我正在开发一个简单的Android应用程序,它创建一个带有checkbox的ListView,如下所示

listAdapter = new ArrayAdapter(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 } */ 

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

请告诉我如何处理这个问题。 任何帮助将不胜感激。

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

我在项目点击后检查我的checkbox的选中状态(例如,在活动关闭或保存按钮点击时)。

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

编辑:例子

 public class MyMultiChoiceActivity extends Activity implements View.OnClickListener { private ListView list = null; private boolean savedSelectionInvalid = true; private List selectedIds = new ArrayList(); 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.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

(更像是黑客而不是解决方案)

您也可以尝试使用以下内容:

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

因此,您将检查一个标志,指示单击后项目的状态。