Question

My Cursor

    Cursor c= db.query(DBHelper.EXAM_DATA,new String [] {DBHelper.EXAM,DBHelper.FILE}, null,null, null, null, null);

Loop for the Cursor

c.moveToFirst();
    do
    {
        name = c.getString(0);
        file = c.getString(1);
        Toast.makeText(this, name + "   "+file  ,Toast.LENGTH_LONG).show();
        adapter = new SimpleCursorAdapter(this, R.layout.row_reasoning, c, new String [] {file}, new int [] {R.id.txtList});
        LvReasoning.setAdapter(adapter);
    }while(c.moveToNext());

Logcat Showing This Error

01-20 16:33:55.154: E/AndroidRuntime(2426): FATAL EXCEPTION: main
01-20 16:33:55.154: E/AndroidRuntime(2426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.ListReasoning}: java.lang.IllegalArgumentException: column '_id' does not exist

My Table have _id Column but I do not know how to solve this error

Was it helpful?

Solution

c.moveToFirst();
    do
    {
        name = c.getString(0);
        file = c.getString(1);
        Toast.makeText(this, name + "   "+file  ,Toast.LENGTH_LONG).show();
        adapter = new SimpleCursorAdapter(this, R.layout.row_reasoning, c, new String [] {DBHelper.FILE}, new int [] {R.id.txtList});

    }while(c.moveToNext());
    LvReasoning.setAdapter(adapter);

I used this and This is Working. I have passed DBHelper table field name and its Work Fine.. But I really dont know Why that code not worked...

OTHER TIPS

The cursor given to a SimpleCursorAdapter must contain a column named _id. The fact that it exists in the table is not enough, you have to select it so include it in the list of columns returned:

Cursor c= db.query(DBHelper.EXAM_DATA,new String [] {"_id", DBHelper.EXAM, DBHelper.FILE}, null,null, null, null, null);

You also only need to instantiate and set the adapter once. Move these statements outside of the while loop (you can remove the loop entirely if you don't need to toast the contents of each cursor row):

adapter = new SimpleCursorAdapter(this, R.layout.row_reasoning, c, new String [] {file}, new int [] {R.id.txtList});
LvReasoning.setAdapter(adapter);

For each cursor row, the adapter will then map the value of column 'file' to R.id.txtList within R.layout.row_reasoning.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top