Simple Cursor Adapter problem

被刻印的时光 ゝ 提交于 2019-12-02 06:55:59

问题


Here is my code for a simple cursor adapter.

public class CursorList extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DatabaseAdapter da = new DatabaseAdapter(this, "mydb.sqlite");

    da.open();
    Cursor cur = da.fetchAllRecords("Doctors", new String[]{"FirstName"});
    startManagingCursor(cur);

    cur.moveToFirst();
    do {
        Log.v("Info", cur.getString(cur.getColumnIndex("FirstName")));
    } while(cur.moveToNext());

    cur.moveToFirst();

    String[] from = new String[]{"FirstName"};
    int[] to = new int[]{R.id.row};

    SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.row_item, cur, from, to);

    setListAdapter(sca);
    }
}

The data records are displayed correctly in the log, but the code stops working when it reaches the

SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.row_item, cur, from, to);

line.

The error I get is :

ERROR/AndroidRuntime(26746): Uncaught handler: thread main exiting due to uncaught exception
ERROR/AndroidRuntime(26746): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arnab.cursorlist/com.arnab.cursorlist.CursorList}:
java.lang.IllegalArgumentException: column '_id' does not exist

Where am I going wrong?

Why does it give an error that column '_id' doesn't exist? Is it a necessary column which we have to have in our tables?

EDIT:

When I put the cursor related code in a try catch block, something like this:

try {
        SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.row_item, cur, from, to);

        setListAdapter(sca);
    }
    catch(Exception E) {
        Log.v("Error", E.getMessage());
    }

I get the message :

VERBOSE/Error(1026): column '_id' does not exist

@Rasel : Here is the fetchAllRecords method

public Cursor fetchAllRecords(String table, String columns[]) {
    return mDb.query(table, columns, null, null, null, null, null);     
}

回答1:


Why does it give an error that column '_id' doesn't exist? Is it a necessary column which we have to have in our tables?

Yes, if you want to use your database information in a cursor adapter. The adapter uses it for internal purposes. Your table must have an '_id' column, and you must select it in your query (so it is in the Cursor result set). You do not have to actually display it in your ListView.

Revised for Posterity

Instead of actually adding the '_id' column, you can SELECT your own 'id' column as '_id', and it will work just the same.




回答2:


Write Cursor related code in try catch block.Your problem will be solved.Check when created table you typed different column rather than '_id'




回答3:


You need to include the table _id in the projection. the list cursor adapter needs the _id to keep track of the rows. you don't have to actually display it anywhere or use it but the cursor needs to contain that column. also a primary key column named _id is mandatory in every android table.



来源:https://stackoverflow.com/questions/6660072/simple-cursor-adapter-problem

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