tags:

views:

18

answers:

1

Hi,

In my app i have have a Cursor field and in the onStart() method of my Android Service I create it by fetching records from my database. When i look into my cursor in the onStart() method i find a number of records but when i try to use them in my trigger() method it has zero records.

the field private Cursor c;

in onStart() c = dbHelper.fetchAllRecords();

in trigger() c.getCount() returns 0

I didn't close the cursor earlier than in my onDestroy() method

A: 

Most likely the cursor isn't actually "losing records" but rather you may be forgetting to c.movetofirst() before iterating through the records EACH TIME.

In other words, as you iterate through cursor records, the record pointer goes up, up, up, until it goes "past the last record". So in order to iterate through your cursor again, you have to reset the record pointer by executing c.movetofirst().

c.moveToFirst();
while (!c.isAfterLast()) 
  print ("Data: " + c.getString(0));

Then running the same while-loop again won't execute because the cursor's record pointer hasn't been reset:

while (!c.isAfterLast()) 
  print ("Data: " + c.getString(0));

(nothing printed)

So you would actually need to change the second code block to:

c.moveToFirst();
while (!c.isAfterLast()) 
  print ("Data: " + c.getString(0));
Brad Hein