rawContact数据查询

对RawContacts主表可查询以下共18项数据
变量名列名备注
_ID_id这个就是raw_contact_id
CONTACT_IDcontact_id
AGGREGATION_MODEaggregation_mode分组模式
DELETEDdeleted是否已经被删除
TIMES_CONTACTEDtimes_contacted
LAST_TIME_CONTACTEDlast_time_contacted
STARREDstarred
CUSTOM_RINGTONEcustom_ringtone
SEND_TO_VOICEMAILsend_to_voicemail
ACCOUNT_NAMEaccount_name
ACCOUNT_TYPEaccount_type
SOURCE_IDsourceid
VERSIONversion
DIRTYdirty
SYNC1~SYNC1sync1~sync2
注意1:_ID就是raw_contact_id
注意2:当我们查询时,通常是查询没有删除的联系人。所以要加上条件RawContacts.DELETED==0:
RawContacts.DELETED+"=?",newString[]{String.valueOf(0)}
注意3:主表中没有名字的信息.名字在子表StructuredName中。
查询例1
String[]projection=newString[]{
RawContacts._ID,
RawContacts.CONTACT_ID,
};
CursorcontactData=managedQuery(
RawContacts.CONTENT_URI,
null,
RawContacts.DELETED+"=?",
newString[]{String.valueOf(0)},
null);
查询例2
Cursorc=managedQuery(
ContentUris.withAppendedId(RawContacts.CONTENT_URI,rawContactId),
projection,
null,
null,
null);
注意:这里是通过ContentUris.withAppendedId(RawContacts.CONTENT_URI,rawContactId)
生成于rawContactId相对应的URL来进行查询。
查询例3
Tofindrawcontactswithinaspecificaccount,youcaneitherputtheaccountnameandtypeintheselectionorpassthemasqueryparameters.
Thelatterapproachispreferable,especiallywhenyoucanreusetheURI:
这样的话就可以重复利用URL,不用每次都要在SQL语句中指定RawContacts.ACCOUNT_NAME,RawContacts.ACCOUNT_TYPE
UrirawContactUri=RawContacts.URI.buildUpon()
.appendQueryParameter(RawContacts.ACCOUNT_NAME,accountName)
.appendQueryParameter(RawContacts.ACCOUNT_TYPE,accountType)
.build();
Cursorc1=getContentResolver().query(rawContactUri,
RawContacts.STARRED+"<>0",null,null,null);//RawContacts.STARRED不为0
...
Cursorc2=getContentResolver().query(rawContactUri,
RawContacts.DELETED+"<>0",null,null,null);
查询例4
ThebestwaytoreadarawcontactalongwithallthedataassociatedwithitisbyusingtheContactsContract.RawContacts.Entitydirectory.
Iftherawcontacthasdatarows,theEntitycursorwillcontainarowforeachdatarow.Iftherawcontacthasnodatarows,
thecursorwillstillcontainonerowwiththerawcontact-levelinformation.
UrirawContactUri=ContentUris.withAppendedId(RawContacts.CONTENT_URI,rawContactId);
UrientityUri=Uri.withAppendedPath(rawContactUri,Entity.CONTENT_DIRECTORY);
Cursorc=getContentResolver().query(entityUri,
newString[]{RawContacts.SOURCE_ID,Entity.DATA_ID,Entity.MIMETYPE,Entity.DATA1},
null,null,null);
try{
while(c.moveToNext()){
StringsourceId=c.getString(0);
if(!c.isNull(1)){
StringmimeType=c.getString(2);
Stringdata=c.getString(3);
...
}
}
}finally{
c.close();
}
在主表中查询不到name信息,但是可以通过子表StructuredName来得
而且在对子表进行查询时,它会把主表的11项数据链接过来。一般情况下,这些数据已经够用了。
关于子表查询的更多信息可参考《raw contact子表数据查询
下面这个程序将展示如何显示每个人的电话号码。
实例1
packagecom.teleca;
importandroid.app.ExpandableListActivity;
importandroid.content.ContentUris;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.net.Uri;
importandroid.os.Bundle;
importandroid.provider.ContactsContract.RawContacts;
importandroid.provider.ContactsContract.CommonDataKinds.Phone;
importandroid.provider.ContactsContract.CommonDataKinds.StructuredName;
importandroid.provider.ContactsContract.CommonDataKinds.Email;
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;
importandroid.provider.ContactsContract.Data;
publicclassCursorTreeAdapterActivityextendsExpandableListActivity{
privatefinalStringTAG="hubin";
ExpandableListViewmExpandableList;
ExpandableListAdaptermAdapter;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);

//Gettheexpandablelistviewobject
mExpandableList=getExpandableListView();

//Setourexpandablelistadapter
String[]projection=newString[]{
StructuredName.DISPLAY_NAME,
StructuredName.RAW_CONTACT_ID,
};
CursorcontactData=managedQuery(
Data.CONTENT_URI,
null,
Data.MIMETYPE+"='"+StructuredName.CONTENT_ITEM_TYPE+"'",
null,
null);
mAdapter=newCursorTreeAdapterExample(contactData,this);
setListAdapter(mAdapter);
}
classCursorTreeAdapterExampleextendsCursorTreeAdapter{
privateintmGroupIdColumnIndex;
privateLayoutInflatermInflater;
privateContextcontext;
publicCursorTreeAdapterExample(Cursorcursor,Contextcontext){
super(cursor,context);

mGroupIdColumnIndex=cursor.getColumnIndexOrThrow(StructuredName.RAW_CONTACT_ID);
mInflater=(LayoutInflater)context.getSystemService(Context.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(StructuredName.DISPLAY_NAME)));
}
@Override
protectedCursorgetChildrenCursor(CursorgroupCursor){
LongrawContactId=groupCursor.getLong(mGroupIdColumnIndex);
Log.i(TAG,"gg:"+rawContactId);
Cursorc=managedQuery(Data.CONTENT_URI,newString[]{Phone._ID,Phone.NUMBER},
Data.RAW_CONTACT_ID+"=?"+"AND"
+Data.MIMETYPE+"='"+Phone.CONTENT_ITEM_TYPE+"'",
newString[]{String.valueOf(rawContactId)},null);
returnc;
}
@Override
protectedViewnewChildView(Contextcontext,Cursorcursor,booleanisExpanded,ViewGroupparent){
Log.d(TAG,"newChildView");

TextViewview=(TextView)mInflater.inflate(android.R.layout.simple_expandable_list_item_1,parent,false);
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);
returnview;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值