Get correct cursor in CustomCursor Adapter getView()

2.3k views Asked by At

I have a custom cursor adapter where each row consists of a check box, text view and button. The button is initially set as GONE, when the user selects the check box I want the button to display so that the user can click it and delete the corresponding row.

My problem is that in my getView() I cannot get the correct cursor so keep getting cursorOutOfBoundsExceptions.

I also want to remove the bindView() if I don't need it any more.

public class DeleteCampaignCustomCursorAdapater extends CursorAdapter {

protected static final String TAG = null;
DatabaseHelper myDbHelper;
protected SQLiteDatabase db;
private final Activity context;

private LayoutInflater mInflater;
protected ListView mListView;

private Cursor c;
public static int[] checked;

TextView merchantNameView;
Button deleteButton;
CheckBox selectForDelete;
//ImageView moveButton;

public DeleteCampaignCustomCursorAdapater(Context context, Cursor c) {
    super(context, c);
    this.context = (Activity) context;
    mInflater = LayoutInflater.from(context);
    this.c = c;
    initializeChecked();
}

@Override
public void bindView(View view, Context context, final Cursor cursor) {

    /*myDbHelper = new DatabaseHelper(context); //Links to DatabaseHelper class

    merchantNameView = (TextView) view.findViewById(R.id.deleteMerchantNameView);
    deleteButton = (Button) view.findViewById(R.id.deleteButton);
    //selectForDelete = (CheckBox) view.findViewById(R.id.deleteCheckBox);
    //moveButton = (ImageView) view.findViewById(R.id.moveButton);

    merchantNameView.setText(cursor.getString(cursor.getColumnIndex("merchantName")));

    final int  rowID = cursor.getInt(cursor.getColumnIndex("_id"));     
    //final long rowID = mListView.getSelectedItemId();

    if (checked[rowID] == 0) {
        selectForDelete.setChecked(false);
    } else {
        selectForDelete.setChecked(true);
    }


    selectForDelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if ( isChecked )
            {
               // Make deleteButton appear 
               RelativeLayout ll = (RelativeLayout) buttonView.getParent();
               deleteButton = (Button) ll.findViewById(R.id.deleteButton);
               checked[rowID] = 1;
               deleteButton.setVisibility(0);
            }
            else {
                //Hide deleteButton
                RelativeLayout ll = (RelativeLayout) buttonView.getParent();
                deleteButton = (Button) ll.findViewById(R.id.deleteButton);
                checked[rowID] = 1;
                deleteButton.setVisibility(8);
            }
        }
    });

    deleteButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

            if(rowID > 0) {
                //Log.d(TAG, "Button Click. rowID = "+rowID);                   

                myDbHelper.deleteRecordWithID(rowID);
                cursor.requery();
                notifyDataSetChanged();
                initializeChecked();  // re-set the checked array
            }                           
        }
    }); */
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    myDbHelper = new DatabaseHelper(context); //Links to DatabaseHelper class

    if (convertView == null) {
        mInflater = context.getLayoutInflater();
        convertView = mInflater.inflate(R.layout.delete_campaign_row_layout, null);
    }       

    merchantNameView = (TextView) convertView.findViewById(R.id.deleteMerchantNameView);
    deleteButton = (Button) convertView.findViewById(R.id.deleteButton);
    selectForDelete = (CheckBox) convertView.findViewById(R.id.deleteCheckBox);     

    merchantNameView.setText(c.getString(c.getColumnIndex("merchantName")));

    final int  rowID = c.getInt(c.getColumnIndex("_id"));       
    //final long rowID = mListView.getSelectedItemId();

    if (checked[position] == 0) {
        selectForDelete.setChecked(false);
    } else {
        selectForDelete.setChecked(true);
    }

    selectForDelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if ( isChecked )
            {
               // Make deleteButton appear 
               RelativeLayout ll = (RelativeLayout) buttonView.getParent();
               deleteButton = (Button) ll.findViewById(R.id.deleteButton);
               checked[position] = 1;
               deleteButton.setVisibility(0);
            }
            else {
                //Hide deleteButton
                RelativeLayout ll = (RelativeLayout) buttonView.getParent();
                deleteButton = (Button) ll.findViewById(R.id.deleteButton);
                checked[position] = 1;
                deleteButton.setVisibility(8);
            }
        }
    });

    deleteButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

            if(rowID > 0) {
                //Log.d(TAG, "Button Click. rowID = "+rowID);                   

                myDbHelper.deleteRecordWithID(rowID);
                c.requery();
                notifyDataSetChanged();
                initializeChecked();  // re-set the checked array
            }                           
        }
    });

    return convertView;     
}

public void initializeChecked() {
    checked = new int[c.getCount()];
    int i = 0;
    while (i < c.getCount()) {
        checked[i] = 0;
        i++;
    }
}

}

Logcat:

05-12 00:36:23.232: W/KeyCharacterMap(15777): Can't open keycharmap file
05-12 00:36:23.232: W/KeyCharacterMap(15777): Error loading keycharmap file '/system/usr/keychars/synaptics-rmi-touchscreen.kcm.bin'. hw.keyboards.65537.devname='synaptics-rmi-touchscreen'
05-12 00:36:23.232: I/KeyCharacterMap(15777): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-12 00:36:23.332: D/WindowManagerImpl(15777): addView, new view, mViews[1]: com.android.internal.policy.impl.PhoneWindow$DecorView@4053e328
05-12 00:36:23.903: D/View(15777): onTouchEvent: viewFlags: 0x18005001
05-12 00:36:23.903: D/View(15777): onTouchEvent: isFocusable: true, isFocusableInTouchMode: false, isFocused: false; focusTaken: false
05-12 00:36:23.913: D/WindowManagerImpl(15777): finishRemoveViewLocked, mViews[1]: com.android.internal.policy.impl.PhoneWindow$DecorView@4053e328
05-12 00:36:23.973: D/Database(15777): dbopen(): path = /data/data/com.roardog.FidelrBasic/databases/Fidelr_memory, flag = 6, file size = 5120
05-12 00:36:23.973: D/Database(15777): dbopen(): path = /data/data/com.roardog.FidelrBasic/databases/Fidelr_memory, mode: wal, disk free size: 253 M, handle: 0x387968
05-12 00:36:23.983: D/ATRecorder(15777): com.htc.autotest.dlib.RecordEngine in loader dalvik.system.DexClassLoader@40550268
05-12 00:36:23.983: D/WindowManagerImpl(15777): addView, new view, mViews[1]: com.android.internal.policy.impl.PhoneWindow$DecorView@40548d90
05-12 00:36:23.993: D/AndroidRuntime(15777): Shutting down VM
05-12 00:36:23.993: W/dalvikvm(15777): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
05-12 00:36:24.023: E/AndroidRuntime(15777): FATAL EXCEPTION: main
05-12 00:36:24.023: E/AndroidRuntime(15777): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:585)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at com.roardog.FidelrBasic.DeleteCampaignCustomCursorAdapater.getView(DeleteCampaignCustomCursorAdapater.java:125)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.widget.AbsListView.obtainView(AbsListView.java:1449)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1265)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.widget.ListView.onMeasure(ListView.java:1128)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.view.View.measure(View.java:8553)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.view.View.measure(View.java:8553)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.view.View.measure(View.java:8553)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.view.View.measure(View.java:8553)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.view.ViewRoot.performTraversals(ViewRoot.java:915)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.os.Looper.loop(Looper.java:150)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at android.app.ActivityThread.main(ActivityThread.java:4385)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at java.lang.reflect.Method.invokeNative(Native Method)
05-12 00:36:24.023: E/AndroidRuntime(15777):    at java.lang.reflect.Method.invoke(Method.java:507)

05-12 00:36:24.023: E/AndroidRuntime(15777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 05-12 00:36:24.023: E/AndroidRuntime(15777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 05-12 00:36:24.023: E/AndroidRuntime(15777): at dalvik.system.NativeStart.main(Native Method) 05-12 00:36:25.304: D/Process(15777): killProcess, pid=15777 05-12 00:36:25.304: D/Process(15777): dalvik.system.VMStack.getThreadStackTrace(Native Method) 05-12 00:36:25.304: D/Process(15777): java.lang.Thread.getStackTrace(Thread.java:745) 05-12 00:36:25.304: D/Process(15777): android.os.Process.killProcess(Process.java:797) 05-12 00:36:25.304: D/Process(15777): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108) 05-12 00:36:25.304: D/Process(15777): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:854) 05-12 00:36:25.314: D/Process(15777): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:851) 05-12 00:36:25.314: D/Process(15777): dalvik.system.NativeStart.main(Native Method) 05-12 00:36:25.314: I/Process(15777): Sending signal. PID: 15777 SIG: 9

FINAL WORKING SOLUTION

public class DeleteCampaignCustomCursorAdapater extends CursorAdapter {

protected static final String TAG = null;
DatabaseHelper myDbHelper;
protected SQLiteDatabase db;
private final Activity context;

private LayoutInflater mInflater;
protected ListView mListView;

private Cursor c;
public static int[] checked;

TextView merchantNameView;
Button deleteButton;
CheckBox selectForDelete;
//ImageView moveButton;

public DeleteCampaignCustomCursorAdapater(Context context, Cursor c) {
    super(context, c);
    this.context = (Activity) context;
    mInflater = LayoutInflater.from(context);
    this.c = c;
    c.moveToFirst();
    initializeChecked();
}

@Override
public void bindView(View view, Context context, final Cursor cursor) {     

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    c.moveToPosition(position);

    myDbHelper = new DatabaseHelper(context); //Links to DatabaseHelper class

    if (convertView == null) {
        mInflater = context.getLayoutInflater();
        convertView = mInflater.inflate(R.layout.delete_campaign_row_layout, null);
    }       

    merchantNameView = (TextView) convertView.findViewById(R.id.deleteMerchantNameView);
    deleteButton = (Button) convertView.findViewById(R.id.deleteButton);
    selectForDelete = (CheckBox) convertView.findViewById(R.id.deleteCheckBox);     

    merchantNameView.setText(c.getString(c.getColumnIndex("merchantName")));

    final int  rowID = c.getInt(c.getColumnIndex("_id"));

    if (checked[position] == 0) {
        selectForDelete.setChecked(false);
    } else {
        selectForDelete.setChecked(true);
    }

    selectForDelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if ( isChecked )
            {
               // Make deleteButton appear 
               RelativeLayout ll = (RelativeLayout) buttonView.getParent();
               deleteButton = (Button) ll.findViewById(R.id.deleteButton);
               checked[position] = 1;
               deleteButton.setVisibility(0);
            }
            else {
                //Hide deleteButton
                RelativeLayout ll = (RelativeLayout) buttonView.getParent();
                deleteButton = (Button) ll.findViewById(R.id.deleteButton);
                //checked[position] = 1;
                deleteButton.setVisibility(8);
            }
        }
    });

    deleteButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {

            if(rowID > 0) {
                //Log.d(TAG, "Button Click. rowID = "+rowID);                   

                myDbHelper.deleteRecordWithID(rowID); //Call deleteRecord on selected row
                c.requery(); //requery the cursor
                notifyDataSetChanged(); //notify the view the data set has changed 
                initializeChecked();  // re-set the checked array
            }                           
        }
    });

    return convertView;     
}

public void initializeChecked() {
    checked = new int[c.getCount()];
    int i = 0;
    while (i < c.getCount()) {
        checked[i] = 0;
        i++;
    }
}

}

1

There are 1 answers

3
Barak On BEST ANSWER

Add c.moveToFirst(); to your constructor before initializeChecked();.

EDIT

First thing in your getView add this:

c.moveToPosition(position);