根据传递的Cursor值展开/折叠ExpandableListView中的组节点?

时间:2011-06-02 05:42:06

标签: android

我有一个ExpandableListView小部件,它使用基于SimpleCursorTreeAdapter的适配器填充,如下所示。我在SQL数据库中查询ListView的组标题以及是否应该展开或折叠组(在Cursor中返回)的布尔值。然后我将此光标提供给我的适配器,并填充并显示列表。

我唯一不能做的是,根据提供的Cursor中的布尔值展开或折叠ListView中的组节点。我想在初始创建,加载和显示包含此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;
    }
}

感谢您的建议和意见。

2 个答案:

答案 0 :(得分: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);
            }
        }
    }       
}

在此代码中,我展开了日期早于当前日期的所有组。 希望这会有所帮助。

答案 1 :(得分: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() );
    }