Android SimpleCursorAdapter No such column Id

落爺英雄遲暮 提交于 2019-12-31 01:47:26

问题


I have a Listview that I want to populate with information from my SQLite database with and this seemed like the most practical solution. In my debugger it says it's caused by:

IllegalArgumentException No such column. Id does not exist

This is the java file I'm trying to populate it with:

    data        = new MyData(this);
    ListView lv = (ListView) findViewById(R.id.list);

    ListAdapter adapter = new SimpleCursorAdapter(
                                this,
                                R.layout.list, 
                                data.selectData(), 
                                new String[] {
                                    "name",
                                    "title"
                                },
                                new int[] {
                                    R.id.name,
                                    R.id.title
                                }
    );
    lv.setAdapter(adapter);

R.layout.list xml file:

<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
    <TextView android:id="@+id/name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="8dip"/>
    <TextView android:id="@+id/title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>

</LinearLayout>

public Cursor selectData() {

    return db.query("tbl_mydata", new String[] {"name", "abb" }, null, null, null, null, null);
}

回答1:


You are not including _id in your column list for the query you do in getSpinnerData().

FYI,

You must be extending CursorAdapter which demand _id column.

_id is used only in CursorAdapter to determine which column is id. You can override this behavior in CursorAdapter or add alias in query to id.




回答2:


SimpleCursorAdapter always need a _id field .

Your database contains table without id column or id with other column name . so uninstall app . and edit CREATE statement as :

  "CREATE TABLE IF NOT EXISTS contact_data( _id INTEGER PRIMARY KEY AUTOINCREMENT,  something ,............ )



回答3:


It means either column name "name" or "data" does not exist in your database table. Can you publish the table schema? Are these column names available?




回答4:


Actually declaring the _id as suggested above will work "_id INTEGER PRIMARY KEY AUTOINCREMENT", but if you don't need _id to be unique over the life of the table, you can omit the AUTOINCREMENT keyword, as the "PRIMARY KEY" will also autoincrement, but is logically equivalently to (max(_id)+1), which guarantees uniqueness which is really what you want when using the CursorAdapter.



来源:https://stackoverflow.com/questions/8090425/android-simplecursoradapter-no-such-column-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!