jeudi 5 mars 2015

App crashes due to java.lang.IllegalArgumentException: column '_id' does not exist

I am making an app with populating a listview from database. But whenever I run it, app crashes dueto java.lang.IllegalArgumentException: column '_id' does not exist. First I didn't have a field ID. Then I did some googling and found that I have to add _id when using SimpleCursorAdapter .But still the problem remains..



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


Above is my dbhelper class(not all).


And and my Activity



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

Aucun commentaire:

Enregistrer un commentaire