1

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!

1
  • I wonder if this is to do with onCreate and onResume. When I start the activity, I call the database work, which is also called onResume (to repopulate ListViews after changes). Looking at developer.android.com/reference/android/app/Activity.html suggests that onResume is used during onCreate, so I maybe I should be using onRestart? Commented Aug 17, 2012 at 12:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.