I am developing a simple Android app which creates a ListView with a checkbox as follows
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
            } */
This is truly weird. How will the app ever be compatible with both version when they require completely opposite checks?
Please let me know how I can handle this. Any help will be appreciated.
i know the problem, on newer frameworks the onItemClick call before the checkd attribute of the checkbox changed!
i check the checked state of my checkboxes in after item clicking (e.g. on activity close or save button click).
look at list.getCheckedItemPositions()...  it will return a SparseBooleanArray you can use!
EDIT: example
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();
    }
}
The issue can be solved by checking the version of the device.
The documentation recommends you check Build.VERSION.SDK_INT against the values in Build.VERSION_CODES.
Sample usage
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
(More like a hack than a solution though)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With