Android:列_id不存在

时间:2015-12-23 18:38:12

标签: java android sqlite

我目前正致力于将SQLite数据库中的数据加载到ListView。所以我使用SimpleCursorAdapter

这里是活动部分中加载数据并在列表视图中显示的initListView()方法。

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);
}

FeedAdapter类如下。

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);
    }
}

当我尝试打开活动时,应用退出,留下以下消息:java.lang.IllegalArgumentException: column '_id' does not exist

根据LogCat,异常是在super()类的FeedAdapter构造函数部分中引起的。我该怎么解决这个问题?

被修改 这是fetchAllPostLists()方法。

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;
}

0 个答案:

没有答案