I have been a naughty boy and I’ve copied a method from official Notepad application from android developer site, this is my class :
package com.example.prva;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
public class ListView extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
fillData();
}
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = DatabaseManager.getAllData();
startManagingCursor(c);
String[] from = new String[] { DatabaseManager.TABLE_COLUMN_ONE };
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
}
When I try to run this ListActivity I get this error :
01-31 02:39:14.259: E/AndroidRuntime(1845): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prva/com.example.prva.ListView}: java.lang.IllegalArgumentException: column '_id' does not exist
Now I understand this because its true, I do not have the _id column in my database (the notepad application database has it and used it but I have my own database), I just don’t understand where is that column mentioned in my ListActivity class? Where is it being called from so it gives the error?
See the documentation for CursorAdapter:
In your code, you use the SimpleCursorAdapter, which is a derived class, so it appears this statement applies.
Cursors are like iterators or pointers, they contain nothing but a mechanism for transversing the data, they contain no columns themselves.
From another documentation, you can understand the statement above better:
Several ways for you to fix the problem:
Add a column “_id” in your table.
Using alias to get the curosr. For example:
SELECT someid as _id, name, number FROM TABLE1;