我有一个 ExpandableListView 小部件,它是使用基于 SimpleCursorTreeAdapter 的适配器填充的,如下所示。我在我的 SQL 数据库中查询 ListView 的组标题以及是否应展开或折叠组的 bool 值(在 Cursor 中返回)。然后,我将此 Cursor 提供给我的适配器,并填充并显示该列表。
我唯一不能做的是根据提供的 Cursor 中的 bool 值展开或折叠 ListView 中的组节点。我希望在最初创建、加载和显示包含此 ExpandableListView 小部件的 Activity 时发生这种情况。
有没有办法让我的适配器获取 Cursor 对象中提供的 bool 值并使用它们展开或折叠这些组节点?
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;
}
}
感谢您的建议和建议。
最佳答案
是的,有办法! 我有同样的问题,我在我的 Activity 中使用了这段代码:
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);
}
}
}
}
在此代码中,我展开所有日期早于当前日期的组。 希望这会有所帮助。
关于android - 根据传递的 Cursor 值展开/折叠 ExpandableListView 中的组节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6210881/