SimpleCursorTreeAdapter

本文介绍了如何利用SimpleCursorTreeAdapter将cursor数据与XML布局文件中的TextView和ImageView进行高效绑定,实现数据展示。通过定义哪些列对应哪些视图以及XML布局文件来定制显示样式,支持子项和分组的分离XML文件,绑定过程分为两阶段,确保数据与UI的无缝对接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

publicabstractclass
SimpleCursorTreeAdapter
extendsResourceCursorTreeAdapter
java.lang.Object
android.widget.BaseExpandableListAdapter
android.widget.CursorTreeAdapter
android.widget.ResourceCursorTreeAdapter
android.widget.SimpleCursorTreeAdapter
ClassOverview
AneasyadaptertomapcolumnsfromacursortoTextViewsorImageViewsdefinedinanXMLfile.
Youcanspecifywhichcolumnsyouwant,
whichviewsyouwanttodisplaythecolumns,
andtheXMLfilethatdefinestheappearanceoftheseviews.
SeparateXMLfilesforchildandgroupsarepossible.Bindingoccursintwophases.
First,ifaSimpleCursorTreeAdapter.ViewBinderisavailable,setViewValue(android.view.View,
android.database.Cursor,int)isinvoked.
Ifthereturnedvalueistrue,bindinghasoccurred.
IfthereturnedvalueisfalseandtheviewtobindisaTextView,setViewText(TextView,String)isinvoked.
IfthereturnedvalueisfalseandtheviewtobindisanImageView,setViewImage(ImageView,String)isinvoked.
Ifnoappropriatebindingcanbefound,anIllegalStateExceptionisthrown.

<wbr style="line-height:25px"><span style="line-height:25px">SimpleCursorTreeAdapter</span><span style="color:#003366; line-height:25px">继承于ResourceCursorTreeAdapter。<br style="line-height:25px"> 它为cursor和ExpandableListView提供了连接的桥梁。<br style="line-height:25px"> 它提供把cursor的数据绑定到Group和child的布局文件中的控件的功能。绑定的控件只能是TextView和ImageView</span><br style="line-height:25px"> SimpleCursorTreeAdapter的子类:<br style="line-height:25px"> 无<br style="line-height:25px"> 他实现了来自于ResourceCursorTreeAdapter的两个方法:<br style="line-height:25px"> bindChildView,bindGroupView<br style="line-height:25px"> 但使用CursorTreeAdapter还需要实现下面一个方法:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ProtectedMethods abstractCursorgetChildrenCursor(CursorgroupCursor)<br style="line-height:25px"> GetstheCursorforthechildrenatthegivengroup.</span><br style="line-height:25px"><span style="line-height:25px">注:</span>关于cursor的数据到Group和child的布局文件中控件的绑定机制和SimpleCursorAdapter差不多<span style="line-height:25px">。</span><br style="line-height:25px"> 具体可参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792010431621954/" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">SimpleCursorAdapter</a></strong></wbr>

importandroid.app.ExpandableListActivity;
importandroid.content.ContentUris;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.provider.ContactsContract.CommonDataKinds.Phone;
importandroid.widget.ExpandableListAdapter;
importandroid.widget.SimpleCursorTreeAdapter;
publicclassExpandableListSample2XmlextendsExpandableListActivity{
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(groupCursor,
this,
android.R.layout.simple_expandable_list_item_1,//这个是系统的
android.R.layout.simple_expandable_list_item_1,//这个是系统的
newString[]{Phone.DISPLAY_NAME},//Nameforgrouplayouts
newint[]{android.R.id.text1},//用于显示的标签,也是系统的
newString[]{Phone.NUMBER},//Numberforchildlayouts
newint[]{android.R.id.text1});//用于显示的标签,也是系统的
setListAdapter(mAdapter);
}
publicclassMyExpandableListAdapterextendsSimpleCursorTreeAdapter{
publicMyExpandableListAdapter(Cursorcursor,Contextcontext,intgroupLayout,
intchildLayout,String[]groupFrom,int[]groupTo,String[]childrenFrom,
int[]childrenTo){
super(context,cursor,groupLayout,groupFrom,groupTo,childLayout,childrenFrom,
childrenTo);
}
//注意二级项数据在这里取得
@Override
protectedCursorgetChildrenCursor(CursorgroupCursor){
//Giventhegroup,wereturnacursorforallthechildrenwithinthatgroup
//Returnacursorthatpointstothiscontact'sphonenumbers
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);
}
}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值