6

我有一个自定义光标适配器,其中每一行都包含一个复选框、文本视图和按钮。该按钮最初设置为 GONE,当用户选中我希望按钮显示的复选框时,用户可以单击它并删除相应的行。

我的问题是,在我的 getView() 中,我无法获得正确的光标,所以继续获取cursorOutOfBoundsExceptions.

如果我不再需要它,我还想删除 bindView()。

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++;
    }
}

}

日志猫:

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)

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): 发送信号。PID:15777 SIG:9

最终工作解决方案

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++;
    }
}

}

4

1 回答 1

6

之前添加c.moveToFirst();到您的构造函数中initializeChecked();

编辑

在您的 getView 中添加以下内容的第一件事:

c.moveToPosition(position);
于 2012-05-11T14:52:03.517 回答