`
kanwoerzi
  • 浏览: 1645223 次
文章分类
社区版块
存档分类
最新评论

ResourceCursorTreeAdapter

 
阅读更多
publicabstractclass
ResourceCursorTreeAdapter
extendsCursorTreeAdapter
java.lang.Object
android.widget.BaseExpandableListAdapter
android.widget.CursorTreeAdapter
android.widget.ResourceCursorTreeAdapter
AfairlysimpleExpandableListAdapterthatcreatesviewsdefinedinanXMLfile.
YoucanspecifytheXMLfilethatdefinestheappearanceoftheviews.
ResourceCursorTreeAdapter继承于CursorTreeAdapter。
它为cursor和ExpandableListView提供了连接的桥梁。

<wbr style="line-height:25px">它让用户提供Group和child的布局文件id,然后它自己就可以为用户创建Group和child的View<br style="line-height:25px"> ResourceCursorTreeAdapter的子类有<br style="line-height:25px"> SimpleCursorTreeAdapter。<br style="line-height:25px"> 他实现了来自于CursorTreeAdapter的两个方法:<br style="line-height:25px"> newChildView,newGroupView<br style="line-height:25px"> 但使用CursorTreeAdapter还需要实现下面三个方法<wbr style="line-height:25px">:</wbr></wbr>
ProtectedMethods
abstractvoidbindChildView(Viewview,Contextcontext,Cursorcursor,booleanisLastChild)
Bindanexistingviewtothechilddatapointedtobycursor
abstractvoidbindGroupView(Viewview,Contextcontext,Cursorcursor,booleanisExpanded)
Bindanexistingviewtothegroupdatapointedtobycursor.
abstractCursorgetChildrenCursor(CursorgroupCursor)
GetstheCursorforthechildrenatthegivengroup.
:关于bindChildView,bindGroupView,的机制和CursorAdapter差不多。
具体可参考《CursorAdapter
实例1
importandroid.app.ExpandableListActivity;
importandroid.content.ContentUris;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.provider.ContactsContract.CommonDataKinds.Phone;
importandroid.view.View;
importandroid.widget.ExpandableListAdapter;
importandroid.widget.ResourceCursorTreeAdapter;
importandroid.widget.TextView;
publicclassExpandableListSample3XmlextendsExpandableListActivity{
privateintmGroupIdColumnIndex;
privateStringmPhoneNumberProjection[]=newString[]{
Phone._ID,Phone.NUMBER
};
privateExpandableListAdaptermAdapter;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//Queryforpeople
CursorgroupCursor=managedQuery(Phone.CONTENT_URI,
newString[]{Phone._ID,Phone.DISPLAY_NAME},null,null,null);
//CachetheIDcolumnindex
mGroupIdColumnIndex=groupCursor.getColumnIndexOrThrow(Phone._ID);
//Setupouradapter
mAdapter=newMyExpandableListAdapter(this,groupCursor,
android.R.layout.simple_expandable_list_item_1,//这个是系统的
android.R.layout.simple_expandable_list_item_1);//这个是系统的
setListAdapter(mAdapter);
}
publicclassMyExpandableListAdapterextendsResourceCursorTreeAdapter{
publicMyExpandableListAdapter(Contextcontext,Cursorcursor,intgroupLayout,intchildLayout){
super(context,cursor,groupLayout,childLayout);
//TODOAuto-generatedconstructorstub
}
//绑定child项数据到视图
@Override
protectedvoidbindChildView(Viewview,Contextcontext,Cursorcursor,booleanisExpanded){
((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));
}
//绑定group项数据到视图
@Override
protectedvoidbindGroupView(Viewview,Contextcontext,Cursorcursor,booleanisExpanded){
((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)));
}
@Override
protectedCursorgetChildrenCursor(CursorgroupCursor){
//Giventhegroup,wereturnacursorforallthechildrenwithinthatgroup
//Returnacursorthatpointstothiscontact'sphonenumbers
//查询得到child项的Cursor
Uri.Builderbuilder=Phone.CONTENT_URI.buildUpon();
ContentUris.appendId(builder,groupCursor.getLong(mGroupIdColumnIndex));
UriphoneNumbersUri=builder.build();

//ThereturnedCursorMUSTbemanagedbyus,soweuseActivity'shelper
//functionalitytomanageitforus.
returnmanagedQuery(phoneNumbersUri,mPhoneNumberProjection,null,null,null);
}
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics