2011-09-13 19 views
1

我想用SimpleCursorTreeAdapter構建expandableList,但是我的列表沒有創建組,它重複每個子元素作爲組元素。expandableList問題

我的數據都是形成KEY_ROWID,KEY_NAME,KEY_EMAIL,KEY_ILID,KEY_KURUMID,KEY_BOLGEID


我DATAS:

(db,"Asli", "[email protected]", 1, 1, 1); 
(db,"Osman Can", "[email protected]", 1, 1, 1); 
(db,"Abuzer Kadayif", "[email protected]", 1, 2, 1); 

(db,"Emre", "[email protected]", 1, 3, 2); 
(db,"Deniz", "[email protected]", 1, 4, 2); 

(db,"Simon Garfunkel", "[email protected]", 1, 5, 0); 
(db,"Eric Clapton", "[email protected]", 1, 6, 0); 
(db,"Neil Young", "[email protected]", 1, 5, 0); 

,這是我的代碼:

String sqlString = "SELECT * " + " FROM " + Items.DATABASE_TABLE; 

     Cursor mCursor = db.rawQuery(sqlString, null); 

     SimpleCursorTreeAdapter mAdapter = new SimpleCursorTreeAdapter(this, 
       mCursor, R.layout.row, R.layout.exprow, 
       //createGroupList(), 
       new String[] { Items.KEY_BOLGEID }, 
       new int[] { R.id.txtItem }, 
       R.layout.exprow, R.layout.exprow, 
       new String[] { Items.KEY_EMAIL,Items.KEY_NAME }, new int[] { R.id.dscItem, R.id.manuItem }) { 

      @Override 
      protected Cursor getChildrenCursor(Cursor groupCursor) { 

       String tempGroup = groupCursor.getString(groupCursor 
         .getColumnIndex(Items.KEY_BOLGEID)); 

       DbHelper dbh = new DbHelper(BrowseActivity.this); 
       SQLiteDatabase db = dbh.getWritableDatabase(); 

       String sqlString = "SELECT " + Items.KEY_ROWID + ", " 
         + Items.KEY_EMAIL + ", " + Items.KEY_NAME + ", " 
         + Items.KEY_KURUMID + ", " + Items.KEY_BOLGEID + ", " 
         + Items.KEY_ILID + " FROM " + Items.DATABASE_TABLE 
         + " WHERE " + Items.KEY_BOLGEID + "= " + tempGroup 
         ; 
       Cursor mCursor = db.rawQuery(sqlString, null); 

       return mCursor; 

      } 


     }; 

     browseView.setAdapter(mAdapter); 

    } 

我的結果應該是:

Group 1: 
(db,"Asli", "[email protected]", 1, 1, 1); 
(db,"Osman Can", "[email protected]", 1, 1, 1); 
(db,"Abuzer Kadayif", "[email protected]", 1, 2, 1); 
Group 2:   
(db,"Emre", "[email protected]", 1, 3, 2); 
(db,"Deniz", "[email protected]", 1, 4, 2); 
Group 3:   
(db,"Simon Garfunkel", "[email protected]", 1, 5, 0); 
(db,"Eric Clapton", "[email protected]", 1, 6, 0); 
(db,"Neil Young", "[email protected]", 1, 5, 0); 

但它是 3次組1 2次組2 3次組0

我試圖改變groupCursor的SQL,但它崩潰。

+0

你還需要方向嗎? – Barak

+0

我正在做類似這裏的東西http://stackoverflow.com/questions/10611927/simplecursortreeadapter-and-cursorloader – toobsco42

回答

0

您需要將您的第一條SQL語句更改爲僅返回您想要的組。現在你得到每個記錄,所以適配器,如果給你一個組每個。改變你的SQL語句:

String sqlString = SELECT DISTINCT " + Items.KEY_BOLGEID + " AS _id FROM " + Items.DATABASE_TABLE; 

,將讓你要組對每個項目的一個實例(並將其設置爲_id列,這是需要適配器)。

然後只在適配器調用的組部分使用new String[] { _id }