Android 简单游标适配器和列表视图

Android 简单游标适配器和列表视图,android,listview,simplecursoradapter,Android,Listview,Simplecursoradapter,无法使用SimpleCursorAdapter生成ListView 这是我在数据处理程序中的游标方法: public Cursor getAllRows() { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(true,TABLE_ITEM,new String[]{KEY_NAME,KEY_VALUE},null,null,null,null,null,null); if(

无法使用SimpleCursorAdapter生成ListView 这是我在数据处理程序中的游标方法:

public Cursor getAllRows()
{
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(true,TABLE_ITEM,new String[]{KEY_NAME,KEY_VALUE},null,null,null,null,null,null);
    if(cursor != null)
    {
        cursor.moveToFirst();
    }
    return cursor;
}
这是我在活动中的列表填充方法:

public void makeList()
{
    Cursor cursor = db.getAllRows();
    String[] fromField = new String[]{"item_name", "item_name"};
    int[] toView = new int[]{R.id.tvName, R.id.tvVal};
    SimpleCursorAdapter sca = new SimpleCursorAdapter(this,R.layout.content_layout,cursor,fromField,toView,0);
    ListView lv = (ListView) findViewById(R.id.listView8);
    lv.setAdapter(sca);
}
并在此处调用此方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_status);
    makeList();
}
运行后,emulator/device中的消息不幸停止,这是由日志cat错误引起的:

09-04 14:55:48.455  32680-32680/com.alright.plastic.dailyex E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.alright.plastic.dailyex, PID: 32680
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alright.plastic.dailyex/com.alright.plastic.dailyex.StatusActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
        at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
        at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
        at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:151)
        at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:93)
        at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:89)
        at com.alright.plastic.dailyex.StatusActivity.makeList(StatusActivity.java:109)
        at com.alright.plastic.dailyex.StatusActivity.onCreate(StatusActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

有关详细信息:

线索位于日志cat错误的顶部:列“\u id”不存在。有关解决方案,请参阅。基本上,CursorAdapter需要结果集中的列_id才能工作,所以只需将其添加到列列表中即可。如果您的表没有_id,那么从长远来看可以更轻松地添加它,或者使用rawQuery选择数据,将rowid别名为_id