Android ExpandableListView中的不同值

时间:2010-09-26 21:51:22

标签: android sqlite cursor distinct expandablelistview

我在使用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 |名字|债券| IR
1 |名字1 |债券1 | ir 1
2 |名字2 |债券1 | ir 2
3 |名称3 |债券2 | ir 3
4 |名字4 |债券3 | ir 4

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

1 个答案:

答案 0 :(得分:0)

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

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

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