开发者

Two Cursors, ListView, CursorIndexOutOfBoundsException

开发者 https://www.devze.com 2023-03-08 05:17 出处:网络
As a beginner in android java world I need your help. I\'ve got problem with famous \"CursorIndexOutOfBoundsException\".

As a beginner in android java world I need your help. I've got problem with famous "CursorIndexOutOfBoundsException".

I'm using SQLite db, and I have two cursors, I'm getting some rows from database. I need to get some previous values with some conditions with second cursor (c2) and put these values on ListView.

Code works with one exception: "android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0".

ListView looks OK if I just ignore this exception but I want to fix it.

I know it is connected to with Cursor. I tried开发者_StackOverflow社区 to check some conditions - it didn't help. Maybe if you could take a look at my code you find where is the cause.

Code:

public void LoadLogGrid()
{
    dbHelper=new DatabaseHelper(this);
    try
    {

    int LogName = (int) spinLog.getSelectedItemId();
    Cursor c=dbHelper.getLogByLogID(LogName);
    if (c != null) c.moveToFirst();
    int count = c.getCount();


    if (c.moveToFirst()){
        ArrayList<String> mArrayList = new ArrayList<String>();

    int i=0;
    do {

        int sVar1 = c.getInt(c.getColumnIndex("Var1")); 
        Long sId = (long) c.getInt(c.getColumnIndex("_id"));            

        Cursor c2=dbHelper.getPrevLogByLogID(LogName,sVar1);    
        c2.moveToFirst();

        if (c2!=null) {
            String sPrevOdo = c2.getString(c2.getColumnIndex("Odo"));
                mArrayList.add(sPrevOdo);
               c2.close();
        } else {
              //stopManagingCursor(c2); 
              //c2.close();
            Log.d("A:", "Something");
        }


        String [] from=new String []{"Date","Col1","Col2","Col3"};
        int [] to=new int [] {R.id.logDate,R.id.logCol1,R.id.logCol2,R.id.logCol3,R.id.rowOpt2};
        SimpleCursorAdapter sca=new LogCursorAdapter(this,R.layout.loggridrow,c,from,to,mArrayList);
        grid.setAdapter(sca);   
        registerForContextMenu(grid);
        i++;    

    } while (c.moveToNext());

        c.close();

    dbHelper.close();
    }   

    }
    catch(Exception ex)
    {
        AlertDialog.Builder b=new AlertDialog.Builder(this);
        b.setMessage(ex.toString());
        b.show();
    }

}

Query in second cursor:

public Cursor getPrevLogByLogID(long LogID, long Var1)
     {
         SQLiteDatabase db=this.getReadableDatabase();
        String[] params=new String[]{String.valueOf(LogID),String.valueOf(Var1)};

         Cursor c2=db.rawQuery("SELECT LogID as _id, Col1 from Log WHERE Col2=? AND Col3<? AND Full=1 ORDER BY Odo DESC", params);
         if (c2 != null) { c2.moveToFirst();}
         return c2;
     }  


Try changing your

do {
    ....
} while (c.moveToNext());

to

while (c.moveToNext()) {
    ....
}

The way it is now, it will run the loop at least once no matter what.


you have moved the c2 to the position as c2.moveToFirst() and after that you are doing the checking process whether it is null or not and i think the cursor should be null so that the exception is raised try putting the checking condition before moving the cursor to the first position

0

精彩评论

暂无评论...
验证码 换一张
取 消