android - Every time i open an Activity, the Cursor moves to next record -


my item "title", items number not fixed, can increase since user put more titles inside database through edittext.

now test purpose put 3 items/titles inside database, here code i'm using inside activityb oncreate(), every time move activitya activityb log prints first item inside database.. i'd take next item instead, , when @ end take first 1 , go again.

/** next title database */     databaseconnector dbconnector = new databaseconnector(activityb.this);     dbconnector.open();     cursor c = dbconnector.listallnotes();      if (c != null && c.movetonext()) {         var = c.getstring(c.getcolumnindex("title"));          log.d("this:", c.getstring(c.getcolumnindex("title")));      }else if(c != null){         c.movetofirst();         var = c.getstring(c.getcolumnindex("title"));          log.d("this:", c.getstring(c.getcolumnindex("title")));      }else{         toast.maketext(this, "no data in database yet", toast.length_long).show();     }     dbconnector.close(); 

on request databaseconnector.listallnotes() @ variables title variable declared follow: private static final string title = "title";

// list data function public cursor listallnotes() {     return database.query(table_name, new string[] { id, title }, null,             null, null, null, title); } 

i think assuming query() gives same cursor object got before updates have rows items inserted since last time queried. not case.

every call listallnotes() runs new query on database, , each query gives new cursor. new cursor has rows of table , positioned before first row. code moves next position (i.e. first row) , reads title, first title in database.

you need either query database once , hold onto cursor, or need remember position last looked @ , use cursor.movetoposition() rather movetonext().


Comments