mardi 14 avril 2015

How to check cursor null or not in android?

I'm trying to check values of field ID in BangCauHoi table. This is my function:



public boolean checkID(String ndCauHoi){
boolean check = true;
String query = "select ID from BangCauHoi where CauHoi=?";
Cursor c = database.rawQuery(query, new String[] { ndCauHoi });
if (c != null)
{
c.moveToFirst();
if (c.isNull(c.getColumnIndex("ID")))
{
check = false;
}
}
c.close();
return check;
}


And result:



java.lang.RuntimeException: Unable to start activity ComponentInfo{us.datn_02/us.datn_02.ShowQuestion}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0


Somebody can help me please?


if I change my code to:



public boolean checkID(String ndCauHoi){
boolean check = true;
String query = "select ID from BangCauHoi where CauHoi=?";
Cursor c = database.rawQuery(query, new String[] { ndCauHoi });
if (c.moveToFirst())
{
if (c.isNull(c.getColumnIndex("ID")))
{
check = false;
}
}
c.close();
return check;
}


then it always return true even field ID have no value with this where clause.


Help me please!... Sorry because my english.


Aucun commentaire:

Enregistrer un commentaire