8

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

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
            } */

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

请让我知道我该如何处理。任何帮助将不胜感激。

4

3 回答 3

3

我知道问题所在,在较新的框架上onItemClick,复选框的 checkd 属性更改之前的调用!

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

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();
    }
}
于 2012-12-19T07:10:58.283 回答
2

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

该文档建议您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

(虽然更像是一个黑客而不是一个解决方案)

于 2012-12-19T07:17:30.003 回答
0

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

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

结果,您有一个 flag checked,它指示单击后项目的状态。

于 2014-10-08T14:05:28.443 回答