简体   繁体   中英

Android: Unable to populate ListView from raw query

I am trying to update a listview from a raw query statement, however I am getting a few errors.

I have my method in my databaseAdapter class:

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

        return c;
    }

And then I have my listview method:

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

I then call that method from an onClickListener and I get the following in my 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)

change the following to:

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

    return c;
}

to

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

Added _id in the select, also ensure that DATABASE_TABLE has the column _id.

A cursor adapter will always need an _id column!

Hope that helps.!

double check is there a column named '_id' in your table,and change the query to

ourDatabase.rawQuery("SELECT _id, number, status FROM "
            + DATABASE_TABLE + " WHERE status = 'open'", null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM