2010-09-26 42 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

相關問題