public abstract class CusrorTreeAdapter extends BaseExpandableListAdpater implements Filterable java.lang.Object android.widget.BaseExpandableListAdapter android.widget.CursorTreeAdapter
直接子类
ResourceCursorTreeAdapter
间接子类
SimpleCursorTreeAdapter
通过该适配类可以用一连续的游标(Coursor)对象访问数据库,并将查询出来的数据展示到可伸缩的列表视图(ExpandableListView)部件上。顶层游标(Cursor)对象(在构造器中指定)显示全部组,后面的游标(Cursor)对象从getChildrenCursor(Cursor)获取并展示子元素组。其中游标携带的结果集中必须有个名为"_id"的列,否则这个类不起任何作用。
使用CursorTreeAdapter需要实现下面五个方法:
受保护方法
abstract void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) abstract void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) abstract Cursor getChildrenCursor(Cursor groupCursor) abstract View newChildView(Context context, Cursor cursor, boolean isLastChild, ViewGroup parent) abstract View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent)
注:关于bindChildView,bindGroupView,newChildView,newGroupView其实的机制和CursorAdapter差不多。
public CursorTreeAdapter (Cursor cursor, Context context)
构造函数。每当数据库的数据发生改变时,适配器将调用requery()重新查询以显示最新的数据。
参数
public CursorTreeAdapter (Cursor cursor, Context context, boolean autoRequery)
构造函数。
参数
public void changeCursor (Cursor cursor)
更改相关的游标为新传入的游标。如果游标已经存在则先关闭这个已存在的游标。
参数
public CharSequence convertToString (Cursor cursor)
将cursor转换成CharSequence。子类应该重写这个方法并转换它们的结果。这个方法的默认实现是:当cursor为空时返回一个空串,否则直接返回调用cursor的toString()方法。
参数
返回值
返回CharSequence类型的值(译者注:这个值是通过转换参数cursor类型而获得的。)
public Cursor getChild (int groupPosition, int childPosition)
获取指定组中的指定子元素的相关数据。
参数
返回值
返回指定子元素数据。
public long getChildId (int groupPosition, int childPosition)
获取指定组中的指定子元素ID,这个ID在组里一定是唯一的。联合ID(参见getCombinedChildId(long, long))在所有条目(所有组和所有元素)中也是唯一的。
参数
返回值
子元素关联ID。
public View getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
获取一个视图对象,显示指定组中的指定子元素数据。
参数
返回值
指定位置上的子元素返回的视图对象
public int getChildrenCount (int groupPosition)
获取指定组中的子元素个数
参数
返回值
返回组中的子元素个数
public Cursor getCursor ()
返回当前适配器绑定的Cursor对象。
返回值
Cursor对象。
public Filter getFilter ()
返回一个可以通过一种过滤模式来约束数据的过滤器。
这个方法通常在Adapter类中实现。
返回值
返回一个用来限制数据的过滤器(Filter)对象。
public FilterQueryProvider getFilterQueryProvider ()
返回一个提供过滤的查询过滤器。若过滤器为null,则不再过滤。
返回值
返回当前过滤器对象,如果不存在返回null。
参考
getFilterQueryProvider()
public Cursor getGroup (int groupPosition)
获取指定组中的数据
参数
返回值
返回组中的数据,也就是该组中的子元素数据。
public int getGroupCount ()
获取组的个数
返回值
组的个数
public long getGroupId (int groupPosition)
获取指定组的ID,这个组ID必须是唯一的。联合ID(参见getCombinedGroupId(long))在所有条目(所有组和所有元素)中也是唯一的。
参数
返回值
返回组ID
public View getGroupView (int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
获取显示指定组的视图对象。这个方法仅返回关于组的视图对象,要想获取子元素的视图对象,就需要调用getChildView(int, int, boolean, View, ViewGroup)。
参数
返回值
返回指定组的视图对象
public boolean hasStableIds ()
组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
返回值
返回一个Boolean类型的值,如果为TRUE,意味着相同的ID永远引用相同的对象。
public boolean isChildSelectable (int groupPosition, int childPosition)
是否选中指定位置上的子元素。
参数
返回值
是否选中子元素
public void notifyDataSetChanged (boolean releaseCursors)
通知数据集已改变,该方法还为任何带有缓存功能的游标提供不用释放资源的可选功能。
参数
public void notifyDataSetChanged ()
该方法内部实现了在每个观察者上面调用onChanged事件。每当发现数据集有改变的情况,或者读取到数据的新状态时,就会调用此方法。
public void notifyDataSetInvalidated ()
该方法内部实现了在每个观察者上面调用onInvalidated事件。每当发现数据集监控有改变的情况,比如该数据集不再有效,就会调用此方法。
public void onGroupCollapsed (int groupPosition)
当指定组收缩状态的时候调用此方法。
参数
public Cursor runQueryOnBackgroundThread (CharSequence constraint)
执行含指定约束的查询。此查询依赖于适配器的过滤器。查询是由FilterQueryProvider提供。如果没有指定FilterQueryProvider,当前cursor不过滤只返回。该方法会通过changeCursor(Cursor)方法返回一个Cursor对象,并且关闭掉先前的Cursor对象。这个方法始终在后台线程执行,而不是在应用程序的主线程(或是UI线程)中运行。规定:当参数constraint为null或为空时,该方法返回原始结果。
参数
返回值
返回含有新的查询结果的Cursor。
参考
getFilter()
getFilterQueryProvider()
setFilterQueryProvider(android.widget.FilterQueryProvider)
public void setChildrenCursor (int groupPosition, Cursor childrenCursor)
为指定组子元素设置游标(Cursor),并关闭已存在的游标。这个方法是非常有用的,当进行异步查询操作的时候用来避免UI发生阻塞的情况。
参数
public void setFilterQueryProvider (FilterQueryProvider filterQueryProvider)
设置一个过滤器,用来过滤当前的Cursor对象。当这个适配器需要进行过滤操作时, runQuery(CharSequence)方法被调用。
参数
参考
getFilterQueryProvider()
runQueryOnBackgroundThread(CharSequence)
public void setGroupCursor (Cursor cursor)
设置组游标对象。
参数