I'm currently working on loading the data from SQLite database to ListView. So I'm using the SimpleCursorAdapter
.
Here's the initListView()
method in the activity part that loads data and shows in the list view.
private void initListView() {
Cursor cursor = db.fetchAllPostLists();
String[] columns = {
DataKeyLists.KEY_UPLOADER,
DataKeyLists.KEY_CONTENT,
DataKeyLists.KEY_UPLOADED_AT
};
int[] to = {
R.id.tvFeedUsername,
R.id.tvFeedText,
R.id.tvFeedCreated,
};
FeedAdapter adapter = new FeedAdapter(this, R.layout.feed_item, cursor, columns, to, 0);
listView.setAdapter(adapter);
}
the FeedAdapter
class is as follows.
public class FeedAdapter extends SimpleCursorAdapter {
public FeedAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags) {
super(context, layout, cursor, from, to, flags);
this.context = context;
inflater = LayoutInflater.from(context);
}
}
When I try to open the activity, the app quits, leaving this message: java.lang.IllegalArgumentException: column '_id' does not exist
According to the LogCat, the exception is caused in the super()
constructor part in the FeedAdapter
class. How should I solve this?
Edited
This is the fetchAllPostLists()
method.
public Cursor fetchAllPostLists() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_POST, new String[] {KEY_POST_ID, KEY_UPLOADER, KEY_CONTENT, KEY_PRIVACY_LEVEL, KEY_UPLOADED_AT, KEY_EDITED_AT}, null, null, null, null, null);
if(cursor != null) {
cursor.moveToFirst();
}
db.close();
return cursor;
}