我正在尝试从原始查询语句更新列表视图,但是我遇到了一些错误。

我在我的databaseAdapter类中有我的方法:

public Cursor getOpenLockers() {
        Cursor c = ourDatabase.rawQuery("SELECT number, status FROM "
                + DATABASE_TABLE + " WHERE status = 'open'", null);
        if (c != null) {
            c.moveToFirst();
        }

        return c;
    }


然后我有我的listview方法:

private void loadListOpenLockers() {
        // TODO Auto-generated method stub
        Locker getList = new Locker(this);
        getList.open();
        Cursor cursor = getList.getOpenLockers();
        startManagingCursor(cursor);
        // setup mapping
        String[] fromFieldNames = new String[] { getList.KEY_LOCKERNUMBER,
                getList.KEY_STATUS };

        int[] toViewIDs = new int[] { R.id.tvLockerNumber, R.id.tvSatus };

        // create adapter
        SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,
                R.layout.itemlayout, cursor, fromFieldNames, toViewIDs);

        // set the adapter
        ListView myList = (ListView) findViewById(R.id.lvInfo);
        myList.setAdapter(myCursorAdapter);
    }


然后,我从onClickListener调用该方法,并在logcat中得到以下内容:

> 04-03 21:35:07.695: E/AndroidRuntime(29384): FATAL EXCEPTION: main
> 04-03 21:35:07.695: E/AndroidRuntime(29384): Process:
> com.********, PID: 29384 04-03 21:35:07.695:
> E/AndroidRuntime(29384): java.lang.IllegalArgumentException: column
> '_id' does not exist 04-03 21:35:07.695: E/AndroidRuntime(29384):     at
> android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:309)
> 04-03 21:35:07.695: E/AndroidRuntime(29384):  at
> android.widget.CursorAdapter.init(CursorAdapter.java:172) 04-03
> 21:35:07.695: E/AndroidRuntime(29384):    at
> android.widget.CursorAdapter.<init>(CursorAdapter.java:120) 04-03
> 21:35:07.695: E/AndroidRuntime(29384):    at
> android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
> 04-03 21:35:07.695: E/AndroidRuntime(29384):  at
> android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
> 04-03 21:35:07.695: E/AndroidRuntime(29384):  at
> com.samsunglockercenter.MainActivity.loadListOpenLockers(MainActivity.java:130)
> 04-03 21:35:07.695: E/AndroidRuntime(29384):  at
> com.samsunglockercenter.MainActivity.onClick(MainActivity.java:161)
> 04-03 21:35:07.695: E/AndroidRuntime(29384):  at
> android.view.View.performClick(View.java:4480) 04-03 21:35:07.695:
> E/AndroidRuntime(29384):  at
> android.view.View$PerformClick.run(View.java:18673) 04-03
> 21:35:07.695: E/AndroidRuntime(29384):    at
> android.os.Handler.handleCallback(Handler.java:733) 04-03
> 21:35:07.695: E/AndroidRuntime(29384):    at
> android.os.Handler.dispatchMessage(Handler.java:95) 04-03
> 21:35:07.695: E/AndroidRuntime(29384):    at
> android.os.Looper.loop(Looper.java:157) 04-03 21:35:07.695:
> E/AndroidRuntime(29384):  at
> android.app.ActivityThread.main(ActivityThread.java:5872) 04-03
> 21:35:07.695: E/AndroidRuntime(29384):    at
> java.lang.reflect.Method.invokeNative(Native Method) 04-03
> 21:35:07.695: E/AndroidRuntime(29384):    at
> java.lang.reflect.Method.invoke(Method.java:515) 04-03 21:35:07.695:
> E/AndroidRuntime(29384):  at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
> 04-03 21:35:07.695: E/AndroidRuntime(29384):  at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885) 04-03
> 21:35:07.695: E/AndroidRuntime(29384):    at
> dalvik.system.NativeStart.main(Native Method)

最佳答案

将以下内容更改为:

public Cursor getOpenLockers() {
    Cursor c = ourDatabase.rawQuery("SELECT number, status FROM "
            + DATABASE_TABLE + " WHERE status = 'open'", null);
    if (c != null) {
        c.moveToFirst();
    }

    return c;
}




public Cursor getOpenLockers() {
    Cursor c = ourDatabase.rawQuery("SELECT _id, number, status FROM "
            + DATABASE_TABLE + " WHERE status = 'open'", null);
    if (c != null) {
        c.moveToFirst();
    }

    return c;
}


在选择中添加了_id,还确保DATABASE_TABLE具有列_id。

游标适配器将始终需要_id列!

希望有帮助!

07-24 16:40