Sunday, April 14, 2013

[android help] exception: cursor index out of range


Here is the code in activity:



//query
final dbhelper helper = new dbhelper(this);
Cursor c = helper.query();
boolean exist =false;
if(c != null && c.moveToFirst()){
Log.d("atestdbChar1no",String.valueOf(c.getCount()));
int i=0;
while(c.isAfterLast()){
Log.d("atestdb1",String.valueOf(i++));
Log.d("atestdb2",String.valueOf(c.getInt(0)));
Log.d("atestdb3",c.getString(1));
Log.d("atestdb4",c.getString(2));
c.moveToNext();
}

//insert
ContentValues values = new ContentValues();
if(!finWords.equals("null")){
if(finWords.length()>index){
String selectWord = finWords.substring(index, index+1);
values.put("character", finChar);
values.put("word", selectWord);
helper.insert(values);


Here is the code in SQLiteOpenHelper:



public void insert(ContentValues values) {
SQLiteDatabase db = getWritableDatabase();
db.insert(TBL_NAME, null, values);
Log.d("test", "dbinsert");
db.close();
}

public Cursor query() {
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.query(TBL_NAME, null, null, null, null, null, null);
Log.d("test", "dbquery");
return c;
}


I can insert data into database but I cannot query them the logcat say the cursor index out of range and just output some data for example just output with id 1,2,4,7 then force close the App but I already insert 14 data what wrong of my code?



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...