cursor index out of bounds “index 0 requested: with size 0”

前端 未结 2 1723
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 02:05

I am getting cursor index out of bounds \"index 0 requested: with size 0\" error when I search my database for something. The item I am searching for in my database does not

相关标签:
2条回答
  • 2020-12-17 02:50

    While you retrive value you have to use cursor.moveToNext;

    if (cursor.moveToFirst()){
       do{
          String data = cursor.getString(cursor.getColumnIndex("data"));
          // do what ever you want here
       }while(cursor.moveToNext());
    }
    
    0 讨论(0)
  • 2020-12-17 03:01

    Cursor.moveToFirst() returns false if the Cursor is empty. The returned Cursor from the query() call will never be null but it might be empty. You are never checking if the cursor is empty.

    public String searchNumber(Context context,String number){
    
        ContactDB db = new ContactDB(context);
        db.open();
        Cursor curs = db.query(DATABASE_TABLE, new String [] {ID,PHONE_NUMBER}
            , PHONE_NUMBER + "='" + number + "'",null,null,null,null);
        String test = null;
        if(curs.moveToFirst()) { //edit
            test = curs.getString(curs.getColumnIndex(db.PHONE_NUMBER)); //fails here
        }
        curs.close();
        db.close();
        return test; // this will be null if the cursor is empty
    }
    

    And get rid of the getIdFromPhone() method.

    0 讨论(0)
提交回复
热议问题