我正在创建一个从数据库填充列表视图的应用程序。但每当我运行它时,应用程序崩溃dueto java.lang.IllegalArgumentException: column '_id'
就不存在了。首先,我没有字段ID。
然后我做了一些谷歌搜索,发现在使用_id
时我必须添加SimpleCursorAdapter
。但问题仍然存在..
private static final String TABLE_CLASS_LIST = "classlist";
//class list column names
private static final String KEY_LIST_ID = "_id";
private static final String KEY_SUBJECT_NAME_LIST = "subjectname";
private static final String KEY_CLASS_DIV_LIST = "classdivision";
private static final String CREATE_CLASS_LIST = "CREATE TABLE "
+ TABLE_CLASS_LIST + "(" + KEY_LIST_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_SUBJECT_NAME_LIST
+ " TEXT," + KEY_CLASS_DIV_LIST + " TEXT)";
public void addClassList(ClassList classList) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SUBJECT_NAME_LIST, classList.getSubject());
values.put(KEY_CLASS_DIV_LIST, classList.getClassd());
// values.put(KEY_CREATED_AT, user.getCreated_at());
// insert row
long user_id = db.insert(TABLE_CLASS_LIST, null, values);
}
public Cursor fetchTableList() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCursor = db.query(TABLE_CLASS_LIST, new String[]{KEY_SUBJECT_NAME_LIST,
KEY_CLASS_DIV_LIST},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
上面是我的dbhelper类(不是全部)。
和我的活动
public class ClassActivity extends ActionBarActivity {
ListView classlist;
String value;
private SimpleCursorAdapter dataAdapter;
DatabaseHelper databaseHelper;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class);
classlist = (ListView) findViewById(R.id.listClass);
databaseHelper = new DatabaseHelper(this);
String[] arrayColumns = new String[]{"classdivision", "subjectname"};
int[] arrayViewIDs = new int[]{R.id.classid, R.id.subject};
cursor = databaseHelper.fetchTableList();
dataAdapter = new SimpleCursorAdapter(
this, R.layout.classlist_single_item,
cursor,
arrayColumns,
arrayViewIDs,
0);
classlist.setAdapter(dataAdapter);
value = getIntent().getExtras().getString("add");
答案 0 :(得分:1)
您的查询的投影缺少_id字段
更改
Cursor mCursor = db.query(TABLE_CLASS_LIST, new String[] { KEY_SUBJECT_NAME_LIST, KEY_CLASS_DIV_LIST},
null, null, null, null, null);
到
Cursor mCursor = db.query(TABLE_CLASS_LIST, new String[] { KEY_LIST_ID, KEY_SUBJECT_NAME_LIST, KEY_CLASS_DIV_LIST},
null, null, null, null, null);