Code example for CheckBox

Methods: getContext, setChecked, setOnCheckedChangeListener, setOnLongClickListener

0
    private void setupCheckbox(CheckBox view, Cursor cursor, final int id) {
        int value = cursor.getInt(ENABLED_COLUMN);
        boolean checked = value > 0;
        clearPreviousListeners(view);
 
        view.setChecked(checked);
        view.setOnLongClickListener(new View.OnLongClickListener() {
            public boolean onLongClick(View view) {
                return false; 
            } 
        }); 
 
        final Context context = view.getContext();
        view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                adapter.update(id, b);
                broadcast.send(context, adapter);
            } 
        }); 
    } 
 
    private void clearPreviousListeners(CheckBox checkbox) {
        // First clear the listener of any recycled views. 
        checkbox.setOnCheckedChangeListener(null);
    } 
 
    // FIX 02/09/2011 Hmm. This doesn't seem very generic or safe. 
    private int getId(Cursor cursor) {
        return cursor.getInt(0);