Получить правильный курсор в CustomCursor Adapter getView ()

У меня есть специальный адаптер курсора, где каждая строка состоит из флажка, текстового представления и кнопки. Кнопка изначально установлена как GONE, когда пользователь устанавливает флажок, который я хочу, чтобы кнопка отображалась, чтобы пользователь мог щелкнуть по ней и удалить соответствующую строку.

Моя проблема в том, что в моем getView () я не могу получить правильный курсор, поэтому продолжайте получатьcursorOutOfBoundsExceptions.

Я также хочу удалить bindView (), если он мне больше не нужен.

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

}

Logcat:

<code>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.3,32: D/WindowManagerImpl(15777): addView, new view, mViews[1]: [email protected]
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]: [email protected]
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 [email protected]
05-12 00:36:23.983: D/WindowManagerImpl(15777): addView, new view, mViews[1]: [email protected]
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)
</code>

05-12 00: 36: 24.023: E / AndroidRuntime (15777): на com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:849) 05-12 00: 36: 24.023: E / AndroidRuntime (15777): на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:607) 05-12 00: 36: 24.023: E / AndroidRuntime (15777): на dalvik.system.NativeStart.main (собственный метод ) 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 (собственный метод) 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 (собственный метод) 05-12 00: 36: 25.314: I / Process (15777): отправка сигнала. PID: 15777 SIG: 9

ФИНАЛЬНОЕ РАБОЧЕЕ РЕШЕНИЕ

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

}

 Roardog11 мая 2012 г., 16:46
Согласно этому / Stackoverflow.com вопросы / 3535074 / ... Не похоже, что вы должны использовать getView () сCustomCursorAdapter, но должен быть какой-то способ получить позицию сbindView() илиnewView()
 Barak11 мая 2012 г., 16:34
не могли бы вы опубликовать LogCat, пожалуйста? Есть несколько вызовов на курсор, нужно знать, какой из них бомбардировки.
 Barak11 мая 2012 г., 16:51
Какая строка является DeleteCampaignCustomCursorAdapater.java:125

Ответы на вопрос(1)

Решение Вопроса

c.moveToFirst(); вашему конструктору доinitializeChecked();.

РЕДАКТИРОВАТ

Первая вещь в вашем getView добавить это:

c.moveToPosition(position);
 Roardog11 мая 2012 г., 16:58
Это устраняет ошибку, но теперь все кампании отображаются в качестве первой. Похоже, курсор не перемещается в следующую позицию, поэтому мне нужно поставитьc.moveToNext() где-то там ...
 Barak11 мая 2012 г., 17:09
Нет, нужно установить позицию курсора при входе в getView. Обновил мой ответ.
 Roardog12 мая 2012 г., 01:15
Спасибо, приятель, ты действительно помог мне!

Ваш ответ на вопрос