Android ExpandableListView 中的不同值

发布于 2024-09-25 04:08:56 字数 1935 浏览 5 评论 0原文

我在使用 ExpandableListView 时遇到问题。我想要的是每个组都来自“bond”列,这是非唯一的,但我希望这些组是唯一的(即“bond”的每个值应该只有一个组)。组中的每个值在其他列中都有应显示的数据。问题是,我似乎可以对一列使用 SQL DISTINCT 运算符(然后 getChildrenCursor() 抛出 IllegalStateException),或者不使用 DISTINCT 运算符并使每个组重复。

谁能建议解决这个问题?

public void onCreate(Bundle saved) {

    super.onCreate(saved);

    DatabaseHelper dh = new DatabaseHelper(this);
    dh.openDataBase();
    db = dh.getReadableDatabase();

    String query = "SELECT DISTINCT bond FROM spectro";

    Cursor c = db.rawQuery(query, null); //throws exception when group opened
    //Cursor c = db.query("spectro", new String[] { "_id", "name", "bond", ir },
        //null, null, null, null, null) - gives duplicate groups

    ExpandableListAdapter a = new IRCursorAdapter (this,
            c, android.R.layout.simple_expandable_list_item_1, new String[] { "bond" },
            new int[] { android.R.id.text1 }, R.layout.row_doubleend,
            new String[] { "name", "ir" }, new int[] { R.id.double_item1, R.id.double_item2 });

    this.setListAdapter(a);


}

protected class IRCursorAdapter extends SimpleCursorTreeAdapter {

    public IRCursorAdapter(Context context, Cursor cursor, int groupLayout,
            String[] groupFrom, int[] groupTo, int childLayout,
            String[] childFrom, int[] childTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom,
                childTo);
    }

    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {

        Cursor c = db.query("spectro", new String[] {"_id", "name", "bond", "ir"},
                "bond=?", new String[] { groupCursor.getString(0) }, null, null, null);

        startManagingCursor(c);
        return c;
    }


};

这就是我的桌子的样子 - 我想要独特的键:

_id |名称 |债券|红外
1 |姓名 1 |债券 1 |红外线1
2 |姓名 2 |债券 1 |红外2
3 |姓名 3 |债券 2 |红外3
4 |名称 4 |债券 3 | ir 4

抱歉,如果这不是特别清楚,但感谢您的帮助!

I'm having trouble with an ExpandableListView. What I want is for each group to be from column "bond", which is non-unique, but I want the groups to be unique (i.e. each value of "bond" should only have one group). Each value in the group has data in other columns which should be displayed. The problem is that it seems I can either use the SQL DISTINCT operator with one column (and then getChildrenCursor() throws an IllegalStateException), or not use the DISTINCT operator and have each group duplicated.

Can anyone suggest a fix for this?

public void onCreate(Bundle saved) {

    super.onCreate(saved);

    DatabaseHelper dh = new DatabaseHelper(this);
    dh.openDataBase();
    db = dh.getReadableDatabase();

    String query = "SELECT DISTINCT bond FROM spectro";

    Cursor c = db.rawQuery(query, null); //throws exception when group opened
    //Cursor c = db.query("spectro", new String[] { "_id", "name", "bond", ir },
        //null, null, null, null, null) - gives duplicate groups

    ExpandableListAdapter a = new IRCursorAdapter (this,
            c, android.R.layout.simple_expandable_list_item_1, new String[] { "bond" },
            new int[] { android.R.id.text1 }, R.layout.row_doubleend,
            new String[] { "name", "ir" }, new int[] { R.id.double_item1, R.id.double_item2 });

    this.setListAdapter(a);


}

protected class IRCursorAdapter extends SimpleCursorTreeAdapter {

    public IRCursorAdapter(Context context, Cursor cursor, int groupLayout,
            String[] groupFrom, int[] groupTo, int childLayout,
            String[] childFrom, int[] childTo) {
        super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom,
                childTo);
    }

    @Override
    protected Cursor getChildrenCursor(Cursor groupCursor) {

        Cursor c = db.query("spectro", new String[] {"_id", "name", "bond", "ir"},
                "bond=?", new String[] { groupCursor.getString(0) }, null, null, null);

        startManagingCursor(c);
        return c;
    }


};

here's what my table looks like- the bond is what I want to be unique:

_id | name | bond | ir
1 | name 1 | bond 1 | ir 1
2 | name 2 | bond 1 | ir 2
3 | name 3 | bond 2 | ir 3
4 | name 4 | bond 3 | ir 4

sorry if this isn't particularly clear, but thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末が日狂欢 2024-10-02 04:08:56

我认为您错误地使用了不同的运算符。 IT 将仅返回 Bond1、Bond2、Bond3,仅此而已。您需要创建复合连接语句来生成正确的游标。你所拥有的总是会抛出错误,因为你没有正确指定子项。其次,我将使用查询(而不是原始查询)并在那里指定参数。

例如 db.query(true, "spectro", new String[]{"bond"}, null,null,...)

您还需要确保您有 Group From -->To 参数和 Children From -->要参数正确!

I think you are mis-using the distinct operator. IT will return only Bond1, Bond2, Bond3 and that's it. You need to create a compound join statement to produce the correct cursor. What you have there will always throw an error because you haven't specified children correctly. Secondly, I would use the query (instead of raw query) and specify parameters there.

E.g. db.query(true, "spectro", new String[]{"bond"}, null,null,...)

You will also need to make sure you have your Group From -->To parameters and Children From --> to parameters correct!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文