使用SimpleCursorAdapter获取错误

时间:2014-07-10 10:52:13

标签: android listview simplecursoradapter

我有Activity使用SimpleCursorAdapter从数据库加载数据。但是每当我测试它时,我都会遇到这样的例外:

07-10 15:53:10.580: D/AndroidRuntime(424): Shutting down VM
07-10 15:53:10.580: W/dalvikvm(424): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-10 15:53:10.620: E/AndroidRuntime(424): FATAL EXCEPTION: main
07-10 15:53:10.620: E/AndroidRuntime(424): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.emplyeerecords/com.example.emplyeerecords.ShowAllData}: java.lang.IllegalArgumentException: column '_id' does not exist
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.os.Looper.loop(Looper.java:123)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-10 15:53:10.620: E/AndroidRuntime(424):  at java.lang.reflect.Method.invokeNative(Native Method)
07-10 15:53:10.620: E/AndroidRuntime(424):  at java.lang.reflect.Method.invoke(Method.java:507)
07-10 15:53:10.620: E/AndroidRuntime(424):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-10 15:53:10.620: E/AndroidRuntime(424):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-10 15:53:10.620: E/AndroidRuntime(424):  at dalvik.system.NativeStart.main(Native Method)
07-10 15:53:10.620: E/AndroidRuntime(424): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.widget.CursorAdapter.init(CursorAdapter.java:111)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.widget.CursorAdapter.<init>(CursorAdapter.java:90)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84)
07-10 15:53:10.620: E/AndroidRuntime(424):  at com.example.emplyeerecords.ShowAllData.onCreate(ShowAllData.java:31)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-10 15:53:10.620: E/AndroidRuntime(424):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-10 15:53:10.620: E/AndroidRuntime(424):  ... 11 more

这是我Activity的代码:

public class ShowAllData extends Activity {
    ListView ls;
    DatabaseHelper helper = new DatabaseHelper(this);
    //adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_all);
        ls = (ListView) findViewById(R.id.listView1);

        //using array adapter
        //ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,helper.getAllName());

        //using SimpleCursorAdapter()
        Cursor c = helper.CursorGetAllName();
        String[] fromCol = new String[]{DatabaseHelper.ID, DatabaseHelper.NAME};
        int[] to = new int[]{R.id.textView1, R.id.textView2};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.show_all_cursor, c, fromCol, to);
        ls.setAdapter(adapter);
    }
}

我的DatabaseHelper

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "EmployeeDetails.db";

    //Table declarations
    public static final String TABLE_NAME = "tblEmp";

    public static final String ID = "empId";
    public static final String NAME = "empName";
    public static final String PHONE = "empPhone";
    public static final String EMAIL = "empEmail";

    private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( " +
            ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            NAME + " VARCHAR(50), " +
            PHONE + " INTEGER(12), " +
            EMAIL + " VARCHAR(100) )";

    SQLiteDatabase db;
    ContentValues values = new ContentValues();
    Cursor c;

    public DatabaseHelper(Context context) {
        super(context, DBNAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    public Cursor CursorGetAllName() {
        Cursor c1 = null;
        db = this.getReadableDatabase();
        c1 = db.query(TABLE_NAME, new String[]{ID, NAME}, null, null, null, null, null);
        if (c1 != null) {
            c1.moveToNext();
        }
        return c1;
    }
}

布局R.layout.show_all

<?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</LinearLayout>

布局R.layout.show_all_cursor

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout> 

我遵循了本教程:tutorialspoint.com

1 个答案:

答案 0 :(得分:1)

错误很明显:

java.lang.IllegalArgumentException: column '_id' does not exist

来自doc"The Cursor must include a column named "_id" or this class will not work"

因此,请将empId更改为_id

再次从文档中"Additionally, using MergeCursor with this class will not work if the merged Cursors have overlapping values in their "_id" columns."

好的是,您还可以使用"as _id"为您自己的ID列创建别名。