Hi,

After you get a cursor object from a query, check the count before
doing "moveToFirst()".

if(cursor.getCount() > 0)
{
   cursor.moveToFirst();
   ... processing ...
}

On May 26, 3:12 am, adek <adrian.ka...@gmail.com> wrote:
> Hello,
>
> 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 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;
>          }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to