0

我想实现一个由 SQL 游标填充的列表视图我已经成功实现了游标并且无法适应它这是我创建游标的 DatabaseHandler

public Cursor fetchAllSuspects() {
         SQLiteDatabase db = this.getReadableDatabase();
          Cursor mCursor = db.query(TABLE_SUSPECTS, new String[] {KEY_ID,
                  KEY_NAME, KEY_SEX}, 
            null, null, null, null, null);

          if (mCursor != null) {
           mCursor.moveToFirst();
          }
          db.close();
          return mCursor;
         }

然后在我的suspectview类中调用它并按如下方式实现

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_suspectview);

    DatabaseHandler db = new DatabaseHandler(this); 
    Cursor lol = db.fetchAllSuspects();
list = (ListView) findViewById(android.R.id.list);

String[] columns = new String[] {
    //  DatabaseHandler.KEY_ID,
        DatabaseHandler.KEY_NAME,
        DatabaseHandler.KEY_SEX         
      };
 int[] to = new int[] { 
         //   R.id.suspect_id,
            R.id.suspect_name,
            R.id.suspect_sex
          };

 dataAdapter = new SimpleCursorAdapter(
            this, 
            R.layout.suspect_list, 
            lol, 
            columns, 
            to,
            0);

 list.setAdapter(dataAdapter);

我在 logcat 中收到错误消息,指出字段 ID 不存在但存在,并且包含 logcat 下面的 6 个数据条目

05-03 19:08:19.951: D/dalvikvm(582): Not late-enabling CheckJNI (already on)
05-03 19:08:20.951: I/dalvikvm(582): threadid=3: reacting to signal 3
05-03 19:08:21.101: I/dalvikvm(582): Wrote stack traces to '/data/anr/traces.txt'
05-03 19:08:21.341: I/dalvikvm(582): threadid=3: reacting to signal 3
05-03 19:08:21.441: I/dalvikvm(582): Wrote stack traces to '/data/anr/traces.txt'
05-03 19:08:21.642: D/dalvikvm(582): GC_FOR_ALLOC freed 120K, 3% free 9333K/9543K, paused 60ms
05-03 19:08:21.802: D/gralloc_goldfish(582): Emulator without GPU emulation detected.
05-03 19:08:21.851: I/dalvikvm(582): threadid=3: reacting to signal 3
05-03 19:08:21.861: I/dalvikvm(582): Wrote stack traces to '/data/anr/traces.txt'
05-03 19:08:58.581: D/AndroidRuntime(582): Shutting down VM
05-03 19:08:58.581: W/dalvikvm(582): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-03 19:08:58.601: E/AndroidRuntime(582): FATAL EXCEPTION: main
05-03 19:08:58.601: E/AndroidRuntime(582): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sherlock/com.example.sherlock.Suspectview}: java.lang.IllegalArgumentException: column '_id' does not exist
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.os.Looper.loop(Looper.java:137)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-03 19:08:58.601: E/AndroidRuntime(582):  at java.lang.reflect.Method.invokeNative(Native Method)
05-03 19:08:58.601: E/AndroidRuntime(582):  at java.lang.reflect.Method.invoke(Method.java:511)
05-03 19:08:58.601: E/AndroidRuntime(582):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-03 19:08:58.601: E/AndroidRuntime(582):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-03 19:08:58.601: E/AndroidRuntime(582):  at dalvik.system.NativeStart.main(Native Method)
05-03 19:08:58.601: E/AndroidRuntime(582): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:267)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.widget.CursorAdapter.init(CursorAdapter.java:168)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.widget.CursorAdapter.<init>(CursorAdapter.java:145)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
05-03 19:08:58.601: E/AndroidRuntime(582):  at com.example.sherlock.Suspectview.onCreate(Suspectview.java:42)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.app.Activity.performCreate(Activity.java:4465)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-03 19:08:58.601: E/AndroidRuntime(582):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-03 19:08:58.601: E/AndroidRuntime(582):  ... 11 more
05-03 19:08:59.001: I/dalvikvm(582): threadid=3: reacting to signal 3
05-03 19:08:59.012: I/dalvikvm(582): Wrote stack traces to '/data/anr/traces.txt'
05-03 19:08:59.552: I/dalvikvm(582): threadid=3: reacting to signal 3
05-03 19:08:59.571: I/dalvikvm(582): Wrote stack traces to '/data/anr/traces.txt'

数据库表

private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "suspectsManager";

// Contacts table name
private static final String TABLE_SUSPECTS = "suspects";

// Contacts Table Columns names
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_SEX = "sex";
public static final String KEY_HEIGHT = "height";
public static final String KEY_AGE = "age";
public static final String KEY_HAIR = "hair";
public static final String KEY_ADD = "additional";
public static final String KEY_NAT = "nationality";
public static final String KEY_TYPE = "type";

可以提供更多代码,谢谢您提前

4

1 回答 1

0

使用系统设置将您更改KEY_ID = "id"KEY_ID = "_id"并更改DATABASE_VERSION为 2 或清除您的应用数据。

于 2013-05-03T19:46:51.030 回答