2010-09-26 54 views
0

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

任何人都可以提出一个解决这个问题吗?

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 |名称| bond | ir
1 |名字1 | bond 1 | ir 1
2 |名称2 | bond 1 | ir 2
3 |名字3 | bond 2 | ir 3
4 |名字4 | bond 3 | ir 4

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

回答

0

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

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

您还需要确保您的Group From - > To参数和儿童从 - >参数正确!

+0

是的,在查询中使用了该组并且完美地工作 – 2011-05-18 13:49:49