SQLiteQueryBuilder setDistinct 不包括 Android Studio 中的所有唯一值 [英] SQliteQueryBuilder setDistinct does not include all unique values in Android Studio

查看:57
本文介绍了SQLiteQueryBuilder setDistinct 不包括 Android Studio 中的所有唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在

这是我的模拟器截图.

事情是新南威尔士未显示在列表中.为什么新南威尔士州是唯一一个似乎没有出现的州.

解决方案

我菜鸟的错误.我有如下游标语句:

c.movetoFirst()

这很好,因为它移动到第一个条目.

但我随后按如下方式遍历数据库:

reefs.moveToNext();做{StateDataProvider dataprovider = new StateDataProvider(reefs.getString(1));arrayList.add(dataprovider);} while (reefs.moveToNext());

我没有删除

reefs.moveToNext()

因为我已经复制并粘贴了该代码.

一旦我删除了

reefs.moveToNext()

一切正常.

I solved an issue in a previous question but now have a new issue based on what I now need to do.

I have about 900 rows in my database and the column in question is called state. For the first page I just want to show the available states a user can tap on that then shows details for just that state on a new activity. So I just select the state from the database and only show one entry for each state. I do this as follows:

public Cursor getData(){
        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        String [] sqlSelect = {"0 _id", "state"};
        String sqlTables = "Reefs";
        qb.setTables(sqlTables);
        qb.setDistinct(true);
        Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
        c.moveToFirst();
        return c;
    }

setDistinct is set to TRUE so only one entry for each state is selected.

Here is my database screenshot and a limited version, but the issue is the same even on my original larger database.

Here is my emulator screenshot.

Thing is New South Wales does not show in the list. Why would New South Wales be the only state that seems to not show up.

解决方案

Rookie mistake by me. I had the cursor statement as follows:

c.movetoFirst()

which is fine as it moves to the first entry.

BUT I then looped through the database as follows:

 reefs.moveToNext();
    do{
        StateDataProvider dataprovider = new StateDataProvider(reefs.getString(1));
        arrayList.add(dataprovider);
    } while (reefs.moveToNext());

I did not remove the

reefs.moveToNext()

as I had copied and pasted that code.

Once I removed the

reefs.moveToNext()

it all works just fine.

这篇关于SQLiteQueryBuilder setDistinct 不包括 Android Studio 中的所有唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆