2011-06-02 29 views
1

我有一個ExpandableListView小部件,它使用基於SimpleCursorTreeAdapter的適配器進行填充,如下所示。我查詢我的SQL數據庫的ListView的組標題以及一個布爾值,以確定該組是否應該展開或摺疊(這是在光標中返回)。然後我將這個光標提供給我的適配器,並且列表被填充和顯示。根據傳遞的Cursor值在ExpandableListView中展開/摺疊組節點?

我唯一不能做的事情是讓ListView中的組節點根據提供的Cursor中的布爾值展開或摺疊。我想在包含此ExpandableListView小部件的活動初始創建,加載和顯示時發生這種情況。

有沒有辦法讓我的適配器採用Cursor對象中提供的布爾值並使用它們展開或摺疊這些組節點?

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter 
    { 
     public MyExpandableListAdapter (Cursor cursor, Context context, int groupLayout, 
             int childLayout, 
           String[] groupFrom, int[] groupTo, 
             String[]childrenFrom,int[]childrenTo) 
     {    
      super(context,cursor,groupLayout,groupFrom,groupTo,childLayout,childrenFrom,childrenTo); 
     } 

    @Override 
    protected Cursor getChildrenCursor(Cursor groupCursor) 
    { 
     String strCategory = 
        groupCursor.getString(groupCursor.getColumnIndex("strCategory")); 

     Cursor cur = mainActivity.mDbHelper.getCardTitlesForCategory(strCategory); 

     return cur; 
    } 
} 

感謝您的建議和意見。

+0

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

回答

0

嘗試此操作(將代碼放置到您的活動onResume()或其他有用的地方)。 ExpandableListView和適配器初始化後,您可以根據遊標值擴展組節點:

Cursor groupCursor = your_adapter.getCursor();   
    if(groupCursor.getCount() > 0 && groupCursor.moveToFirst()) { 
     do { 
      boolean isExpanded = groupCursor.getInt(....) > 0; 
      if(isExpanded) { 
       your_list_view.expandGroup(groupCursor.getPosition()); 
      } 
     } while(groupCursor.moveToNext()); 
    } 
2

是的,有一種方法! 我有同樣的問題,我在活動中使用此代碼:

private void expandGroups() { 
    // ExpandableListView scheduleListView 
    int itemsCount = this.scheduleListView.getCount(); 
    if(itemsCount > 0) { 
     long timeNowSec = (new Date()).getTime()/1000 - ONE_DAY_SEC; 
     Cursor cursor = null; 
     SimpleCursorTreeAdapter adapter = (SimpleCursorTreeAdapter)this.scheduleListView.getExpandableListAdapter(); 
     for(int i=0; i<itemsCount; i++) { 
      cursor = adapter.getGroup(i); 
      long dateSec = cursor.getLong(cursor.getColumnIndex(DbDefinitions.TABLE_PAIRS.FIELD_DATE)); 
      if(dateSec > timeNowSec) { 
       this.scheduleListView.expandGroup(i); 
      } 
      else { 
       this.scheduleListView.collapseGroup(i); 
      } 
     } 
    }  
} 

在這段代碼我在那裏擴大日期之前那麼當前日期的所有羣體。 希望這有助於。