Android SQLite游标循环两次

时间:2012-08-17 09:42:39

标签: cursor android-sqlite

我有一个android游标循环两次的问题,即使我知道cursor.getCount()中只返回了一个值。

我在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;
}

然后我在我的工人类中用以下方法调用它:

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();

但是,当我查看myArrayList的内容时,它有两个相同的sub_id值。我已经尝试过使用c.moveToFirst()等等。但是我仍然在arrayList中得到两个值,即使c.getCount()= 1!

1 个答案:

答案 0 :(得分:1)

最后钉了这个。它与类变量和方法变量有关。我有一个类变量,它由onCreate中的一个方法填充,该方法也在onResume中被调用。解决方案是将变量移动到方法中。愚蠢的错误,易于修复,难以找到(对我来说)。