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 | 红外线 4

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

4

1 回答 1

0

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

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

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

于 2010-11-30T16:15:31.687 回答