Thank you for your answer. Yes. I wasn't sure which way is better - using two cursors or using one cursor with Custom CursorAdapter (and curosr inside custom View in it).
On May 26, 12:27 pm, Kostya Vasilyev <[email protected]> wrote: > I already posted the basic pattern two messages back. > > As for nested data, you might want to use CursorAdapter (with subclassing) > instead of trying to fit nested queries to SimpleCursorAdapter, which is > more trouble than it's worth. > > If you do, you won't need to iterate through the top-level query as a > preliminary step. Your nested query will go into the adapter's bindView > method. > > *http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/<http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/> > * <http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/> > > http://android-mag.blogspot.com/2011/02/android-examples-how-to-use.html > > -- Kostya > > 2011/5/26 Adrian Kajda <[email protected]> > > > > > > > > > Can you give me a link with an example of that? > > > 2011/5/26 Kostya Vasilyev <[email protected]> > > >> You can use the pattern for the top level and the nested query both. > > >> -- Kostya > >> 2011/5/26 adek <[email protected]> > > >>> I tried to change my code this way. There is still a problem. > > >>> I tried to check for empty rows but even then I've got this exception > >>> error. > > >>> Maybe the problem is in my way of dealing with those two cursors > >>> (second cursor is in another) > > >>> Maybe I'll start from the beggining and you will tell me a better way: > > >>> I've got ONE ListView. On this list I'd like to have some values from > >>> database (easy - done with one cursor) > >>> but in some list positions there should be values with some conditions > >>> from another query (second cursor). > > >>> On May 26, 10:18 am, Kostya Vasilyev <[email protected]> wrote: > >>> > A cursor's initial position is -1, right before the first row, so it's > >>> quite > >>> > enough to do this: > > >>> > Cursor cursor = ..... query ... > >>> > if (cursor != null) { > >>> > ... get column indices here .... > >>> > while (cursor.moveToNext()) { > >>> > ... get data using the indices here ... > >>> > } > >>> > cursor.close(); > > >>> > } > > >>> > Here it's not necessary to use getCount or moveToFirst, or handle the > >>> case > >>> > of no data in any special way. > > >>> > -- Kostya > > >>> > 2011/5/26 adek <[email protected]> > > >>> > > Thanks. But this is strange. > >>> > > When I add this condition > > >>> > > if(cursor.getCount() > 0) } > >>> > > ... > > >>> > > My ListView is empty. In LogCat everything looks fine. > > >>> > > On May 26, 9:59 am, Senthil ACS <[email protected]> wrote: > >>> > > > 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 <[email protected]> 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 > >>> [email protected] > >>> > > To unsubscribe from this group, send email to > >>> > > [email protected] > >>> > > For more options, visit this group at > >>> > >http://groups.google.com/group/android-developers?hl=en > > >>> -- > >>> You received this message because you are subscribed to the Google > >>> Groups "Android Developers" group. > >>> To post to this group, send email to [email protected] > >>> To unsubscribe from this group, send email to > >>> [email protected] > >>> For more options, visit this group at > >>>http://groups.google.com/group/android-developers?hl=en > > >> -- > >> You received this message because you are subscribed to the Google > >> Groups "Android Developers" group. > >> To post to this group, send email to [email protected] > >> To unsubscribe from this group, send email to > >> [email protected] > >> For more options, visit this group at > >>http://groups.google.com/group/android-developers?hl=en > > > -- > > pozdrawiam, > > Adrian Kajda > > > -- > > You received this message because you are subscribed to the Google > > Groups "Android Developers" group. > > To post to this group, send email to [email protected] > > To unsubscribe from this group, send email to > > [email protected] > > For more options, visit this group at > >http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

