CursorTreeAdapter

本文深入探讨了CursorTreeAdapter的实现原理与使用方法,并通过一个具体的实例展示其在ExpandableListView中的应用。

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

ClassOverview
AnadapterthatexposesdatafromaseriesofCursorstoanExpandableListViewwidget.
Thetop-levelCursor(thatisgivenintheconstructor)exposesthegroups,
whilesubsequentCursorsreturnedfromgetChildrenCursor(Cursor)exposechildrenwithinaparticulargroup.
TheCursorsmustincludeacolumnnamed"_id"orthisclasswillnotwork.

CursorTreeAdapter继承于BaseExpandableListAdapter,它是个虚类。
它为cursor和ExpandableListView提供了连接的桥梁。
CursorTreeAdapter的子类有
CursorTreeAdapter
ResourceCursorTreeAdapter
SimpleCursorTreeAdapter

使用CursorTreeAdapter需要实现下面五个方法:
ProtectedMethods
abstractvoidbindChildView(Viewview,Contextcontext,Cursorcursor,booleanisLastChild)
Bindanexistingviewtothechilddatapointedtobycursor
abstractvoidbindGroupView(Viewview,Contextcontext,Cursorcursor,booleanisExpanded)
Bindanexistingviewtothegroupdatapointedtobycursor.
abstractCursorgetChildrenCursor(CursorgroupCursor)
GetstheCursorforthechildrenatthegivengroup.
abstractViewnewChildView(Contextcontext,Cursorcursor,booleanisLastChild,ViewGroupparent)
Makesanewchildviewtoholdthedatapointedtobycursor.
abstractViewnewGroupView(Contextcontext,Cursorcursor,booleanisExpanded,ViewGroupparent)
Makesanewgroupviewtoholdthegroupdatapointedtobycursor.
:关于bindChildView,bindGroupView,newChildView,newGroupView其实的机制和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.util.Log;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.ViewGroup.LayoutParams;
importandroid.widget.CursorTreeAdapter;
importandroid.widget.ExpandableListAdapter;
importandroid.widget.ExpandableListView;
importandroid.widget.TextView;
importandroid.widget.Toast;

publicclassExpandableListSample1XmlextendsExpandableListActivity{
privatefinalStringTAG="Expandable1";
ExpandableListViewmExpandableList;
ExpandableListAdaptermAdapter;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//Gettheexpandablelistviewobject
mExpandableList=getExpandableListView();
//Setourexpandablelistadapter
String[]projection=newString[]{
Phone._ID,
Phone.DISPLAY_NAME,
Phone.NUMBER
};
CursorcontactData=managedQuery(
Phone.CONTENT_URI,
projection,
null,
null,
null);
//注意这里也只设置了一级项的数据
mAdapter=newCursorTreeAdapterExample(contactData,this);
setListAdapter(mAdapter);

Toast.makeText(this,"Totalcontacts:"+contactData.getCount(),Toast.LENGTH_LONG);
Log.d("EXPANDABLE","Totalcontacts:"+contactData.getCount());
}
/**
*Adapterimplementation
*@authorwk.kho
*
*/
publicclassCursorTreeAdapterExampleextendsCursorTreeAdapter{
privateintmGroupIdColumnIndex;
privateLayoutInflatermInflater;
//注意这里的游标是一级项的
publicCursorTreeAdapterExample(Cursorcursor,Contextcontext){
super(cursor,context);

mGroupIdColumnIndex=cursor.getColumnIndexOrThrow(Phone._ID);
mInflater=(LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
}
//注意这里的游标是二级项的
@Override
protectedvoidbindChildView(Viewview,Contextcontext,Cursorcursor,booleanisExpanded){
//Bindtherelateddatawiththischildview
((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));
}
@Override
protectedvoidbindGroupView(Viewview,Contextcontext,Cursorcursor,booleanisExpanded){
//Bindtherelateddatawiththisgroupview
((TextView)view).setText(cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)));
}
//注意这里通过一次数据库查询才得到了二级项的数据
@Override
protectedCursorgetChildrenCursor(CursorgroupCursor){
Uri.Builderbuilder=Phone.CONTENT_URI.buildUpon();
ContentUris.appendId(builder,groupCursor.getLong(mGroupIdColumnIndex));

UriphoneNumbersUri=builder.build();

//ThereturnedCursorMUSTbemanagedbyus,soweuseActivity'shelper
//functionalitytomanageitforus.
returnmanagedQuery(phoneNumbersUri,newString[]{Phone._ID,Phone.NUMBER},null,null,null);
}
@Override
protectedViewnewChildView(Contextcontext,Cursorcursor,booleanisExpanded,ViewGroupparent){
Log.d(TAG,"newChildView");

TextViewview=(TextView)mInflater.inflate(android.R.layout.simple_expandable_list_item_1,parent,false);

view.setText("("+cursor.getPosition()+")"+cursor.getString(cursor.getColumnIndex(Phone.NUMBER)));

returnview;
}
@Override
protectedViewnewGroupView(Contextcontext,Cursorcursor,booleanisExpanded,ViewGroupparent){
Log.d(TAG,"newGroupView");
TextViewview=(TextView)mInflater.inflate(android.R.layout.simple_expandable_list_item_1,parent,false);
view.setText("("+cursor.getPosition()+")"+cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)));

returnview;
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值