iaindownie
iaindownie

Reputation: 1056

Android SQLite cursor looping twice

I have a problem with the android cursor looping twice, even though I know there is only one value returned in the cursor.getCount().

I'm using the following in the DBAdapter:

public Cursor getAllSubDetailsFromObsTable() {
Cursor c = mDb
        .rawQuery(
                "SELECT DISTINCT sub.sub_id, sub.complete, mobileobs.date, mobileobs.location, mobileobs.grid_ref, "
                        + "mobileobs.time, mobileobs.protocol_id, mobileobs.endtime FROM sub, mobileobs "
                        + "WHERE sub.sub_id = mobileobs.sub_id",
                null);
return c;
}

Then I call this within my worker class with:

db.open();
Cursor c = db.getAllSubDetailsFromObsTable();
while (c.moveToNext()) {
        String sub_id = c.getString(0);
        //Rest of real code <snipped>
        myArrayList.add(sub_id);
}
c.close();
db.close();

However, when I look at the contents of myArrayList, it has two identical values of sub_id. I have tried mucking about with c.moveToFirst() etc. etc. but I still get two values in the arrayList even though c.getCount() = 1!

Upvotes: 1

Views: 1111

Answers (1)

iaindownie
iaindownie

Reputation: 1056

finally nailed this. It was to do with class variables and method variables. I had a class variable that was being populated by a method within onCreate that was also being called within onResume. Solution was to move the variable into the method. Stupid bug, easy to fix, difficult (for me) to find.

Upvotes: 1

Related Questions