Android(安卓)ApiDemos示例解析(113):Views->Expandable Lists->2. Cursor(People)
上类使用自定义的ExpandableListAdapter使用数组来定义Expandable list 的group 和 childern. 下图为ExpandableListAdapter之间的继承关系
Expandable Lists->2. Cursor(People)_第1张图片" src="https://img.it610.com/image/info5/76cc8860b0d749c9abde00a442da633a.jpg" width="650" height="567" style="border:1px solid black;">
CursorTreeAdapter 支持使用一系列的Cursors 作为ExpandableListView 的数据源,最上层的一个Cursor定义为Expandable的group ,其它的Cursor可以通过getChildernCursor为特定的Group定义其子Item。 所有的Cursor对象必须含有一个列名为”_id”的列,否则本类无法工作。
Cursor的用法可以参见Android ApiDemos示例解析(10):App->Activity->QuickContactsDemo。
CursorTreeAdapter类名中带有“Tree”,和其它平台中的TreeView不同,Expandable List只支持两层。
ResourceCursorTreeAdapter 为CursorTreeAdapter 添加了两个方法 newChildView和newGroupView 其构造函数允许为ExpandableListView 的group 和child 指定XML layout资源。
SimpleCursorTreeAdapter 提供了从表的列映射到TextView 或ImageView (在XML资源中定义)的简便方法。你可以指定需要哪些列,每个列使用哪种View来显示。
本例定义了SimpleCursorTreeAdapter 的子类MyExpandableListAdapter 使用Contact 通讯录作为数据源。
使用通讯录中联系人人名作为Group,而每个联系人对应的所有电话号码作为该group 的childeren.
Cursor groupCursor = managedQuery(People.CONTENT_URI, new String[] {People._ID, People.NAME}, null, null, null);// Cache the ID column indexmGroupIdColumnIndex = groupCursor.getColumnIndexOrThrow(People._ID);// Set up our adaptermAdapter = new MyExpandableListAdapter(groupCursor, this, android.R.layout.simple_expandable_list_item_1, android.R.layout.simple_expandable_list_item_1, new String[] {People.NAME}, // Name for group layouts new int[] {android.R.id.text1}, new String[] {People.NUMBER}, // Number for child layouts new int[] {android.R.id.text1});
其构造函数定义如下:
MyExpandableListAdapter(Cursor cursor, Context context, int groupLayout,
int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
int[] childrenTo) {
- cursor : group Cursor ,本例为联系人名,可以看到此Cursor的列必须有一列为_ID.
- context: context对象,可以使用activity。
- groupLayout: Group Layout资源ID,本例使用系统资源simple_expandable_list_item_1.
- childLayout: child Layout资源ID,本例使用系统资源simple_expandable_list_item_1。
- groupFrom: 需要显示的组列名数组,本例为Name
- groupTo: 对应每个组的列对应的View 的id, 本例为一文本。
- childrenFrom: 需要显示的组成员列名,本例为Number。
- childrenTo:对应每个组成员的列对应的View 的id, 本例为一文本。
Expandable Lists->2. Cursor(People)_第2张图片" src="https://img.it610.com/image/info5/50d32cc9c31b407ea4f1c38e1bfcb960.jpg" width="320" height="480" style="border:1px solid black;">
更多相关文章
- android TabHost(选项卡)的使用方法发布
- Android资源管理中的Runtime Resources Overlay-------之overlay
- android UiAutomator自定义快速调试类
- Android(安卓)用户自定义对话框
- Android(安卓)使用自定义SVG
- Android(安卓)之LayerDrawable层叠样式layer-list及自定义颜色Pr
- Android教程之如何使用自定义字体
- Android中动画实现单击按钮控制开屏关屏效果(系统animation实现
- Android简明开发教程十三:Option Menu 画笔示例