从数据库中取出数据封装成对象:
package com.litsoft.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.CommonDataKinds.Website;
import com.litsoft.domain.Contact2;
public class DB2ContactUtil {
public static ContentResolver resolver;
public static String numberSplit="NO";
public static String emailSplit=";";
/**
* 删除一个
*/
public static void deleteById(String contactId ){
resolver.delete(Data.CONTENT_URI, ContactsContract.Data.CONTACT_ID + "=" + contactId, null);
resolver.delete(RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null);
}
/**
* 删除所有的 Contact
*/
public static void deleteAll(){
resolver.delete(Data.CONTENT_URI, null, null);
resolver.delete(RawContacts.CONTENT_URI, null, null);
}
/**
* 获得所有的Contact
* @return
*/
public static List<Contact2> getAllContact(){
List<Contact2> contactList = new ArrayList<Contact2>();
ArrayList<String> idList = getContactIdList();
Contact2 newContact ;
for(String contactId:idList){
newContact = new Contact2();
newContact.setId(contactId);
//1.头像
newContact.setContactIcon(getContactIcon(contactId));
//2.名字
newContact.setName(getName(contactId));
//3.电话
newContact.setPhoneNumberMap(getPhoneNumber(contactId));
//4.Email
newContact.setEmailMap(getEmail(contactId));
//5.im
newContact.setImMap(getAIM(contactId));
//6.add
newContact.setAddrMap(getAddressDetail(contactId));
//7.organization
newContact.setOrganizationMap(getOrganizations(contactId));
//8.notes
newContact.setNotes(getNotes(contactId));
//9.nickname
newContact.setNicks(getNick(contactId));
//10.website
newContact.setWebsites(getWebsite(contactId));
contactList.add(newContact);
}
return contactList;
}
/**
* 根据id 获得 头像
* @param contactId
* @return
*/
public static byte[] getContactIcon(String contactId){
byte[] image = null;
Cursor cursor = resolver.query(Data.CONTENT_URI, null,
Photo.CONTACT_ID + " = " + contactId + " and " + Data.MIMETYPE
+ " = '" + Photo.CONTENT_ITEM_TYPE+"'", null, null);
if(cursor.moveToNext()){
image = cursor.getBlob(cursor.getColumnIndex(Photo.PHOTO));
}
return image;
}
/**
* 根据Id 得到 familyName 和 giveName [1,0]
*/
public static String[] getName(String contactId){
String name[] = new String[2];
Cursor cursor = resolver.query(Data.CONTENT_URI, null,
StructuredName.CONTACT_ID + " = " + contactId + " and " + Data.MIMETYPE
+ " = '" + StructuredName.CONTENT_ITEM_TYPE+"'", null, null);
while(cursor.moveToNext()){
name[1]= cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME));
name[0] = cursor.getString(cursor.getColumnIndex(StructuredName.GIVEN_NAME));
}
System.out.println("give name :" + name[0] + " famliy name :" + name[1]);
return name;
}
/**
* 获得所有联系人的id列表
*/
public static ArrayList<String> getContactIdList() {
ArrayList<String> contactIdList = new ArrayList<String>();
Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String id = "";
while (cursor.moveToNext()) {
id = cursor.getString(cursor.getColumnIndex(Contacts._ID));
contactIdList.add(id);
System.out.println("contractId is :"+id);
}
return contactIdList;
}
/**
* 根据用户Id 电话号码
*
* @param contactId key{1,2,3,7} ==={home,mobile,work,other}
* @return
*/
public static Map<String,String> getPhoneNumber(String contactId) {
Map<String,String> phoneNumberMap = new HashMap<String, String>();
String value = "";
String key ="";
Cursor phones = resolver.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
key = phones.getString(phones.getColumnIndex(Phone.DATA2));
value = phones.getString(phones.getColumnIndex(Phone.DATA1));
if("7".equals(key)){
String v = phoneNumberMap.get(key);
if(v!=null&&!"".equals(v)){
value = v+numberSplit+ value;
}
}
phoneNumberMap.put(key, value);
System.out.println("number type is :" + key + " number is :" + value);
}
return phoneNumberMap;
}
/**
* 根据用户Id 获得email
* key {1,2,3,4} =={home,work,other, mobile }
*
*/
public static Map<String,String> getEmail(String contactId) {
Map<String,String> emailMap = new HashMap<String, String>();
String key = "";
String value = "";
Cursor emails = resolver.query(Email.CONTENT_URI, null,
Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
key = emails.getString(emails.getColumnIndex(Email.DATA2));
value = emails.getString(emails.getColumnIndex(Email.DATA1));
if("4".equals(key)){
String v = emailMap.get(key);
if(v!=null&&!"".equals(v)){
value = v+emailSplit+ value;
}
}
emailMap.put(key, value);
System.out.println("email type is :" + key + " email is :" + value);
}
return emailMap;
}
/**
* 根据用户Id获得 AIM
* key {0,1,2,3,4,5,6,7} == {aim,live,yahoo,skyp,qq,gtalk,icq,jabber}
* @param contactId
* @return
*/
public static Map<String,String> getAIM(String contactId) {
Map<String,String> im =new HashMap<String,String>();
String key;
String value;
String selection = Im.CONTACT_ID + " = " + contactId //获得联系人的具体id
+ " and " + Data.MIMETYPE + " = '" + CommonDataKinds.Im.CONTENT_ITEM_TYPE+"'";
Cursor cursor = resolver.query(Data.CONTENT_URI, null, selection, null, null);
while(cursor.moveToNext()){
key = cursor.getString(cursor.getColumnIndex(Im.DATA5));//判断im 不同种类
value = cursor.getString(cursor.getColumnIndex(Im.DATA1));
if("7".equals(key)){//jabber类型
String v = im.get(key);
if(v!=null&&!"".equals(v)){
value = v+";"+ value;
}
}
im.put(key, value);
System.out.println("aimType :"+ key +" ; aim : " + value);
}
//System.out.println("aim : " + aim);
return im;
}
//=======================================Map<String,List<String[]>>类型========================================
/**
* 根据用户Id 获得 Address
* data4-data10 : {street pobox neigborbood city state zip country}==str[0-6]
* key {1,2,3} =={home,work,other}
*/
public static Map<String,List<String[]>> getAddressDetail(String contactId) {
Map<String,List<String[]>> postalMap = new HashMap<String, List<String[]>>();
String key;
List<String[]> addrList;
String addr[];
Cursor adds = resolver
.query(StructuredPostal.CONTENT_URI, null,
StructuredPostal.CONTACT_ID + " = " + contactId, null, null);
while (adds.moveToNext()) {
key = adds.getString(adds
.getColumnIndex(StructuredPostal.DATA2));
addr = new String[7];
addr[0] = adds.getString(adds
.getColumnIndex(StructuredPostal.STREET));
addr[1] =adds.getString(adds
.getColumnIndex(StructuredPostal.POBOX));//信箱号码
addr[2] = adds.getString(adds
.getColumnIndex(StructuredPostal.NEIGHBORHOOD));
addr[3] = adds.getString(adds
.getColumnIndex(StructuredPostal.CITY));
addr[4]=adds.getString(adds
.getColumnIndex(StructuredPostal.REGION));// 省份
addr[5] =adds.getString(adds
.getColumnIndex(StructuredPostal.POSTCODE));// 邮政编码
addr[6] = adds.getString(adds
.getColumnIndex(StructuredPostal.COUNTRY));
addrList = postalMap.get(key);
if(addrList!=null&&!addrList.isEmpty()){
addrList.add(addr);
postalMap.put(key, addrList);
}else{
addrList = new ArrayList<String[]>();
addrList.add(addr);
postalMap.put(key, addrList);
}
System.out.println("address's type is :"+ key +" -- address: " + addr[0]+":"+ addr[1]+":"+addr[2]+":"+addr[3]+":"+addr[4]+":"+addr[5]+":"+addr[6]);
}
return postalMap;
}
/**
* 根据用户Id 获得 公司
* organization: Map<String,List> String 是数据类型 List 是一个数组:String [data1(company),data4(position)]
* key {1,2} ==={work ,other}
*/
public static Map<String,List<String[]>> getOrganizations(String contactId) {
Map<String,List<String[]>> companyMap = new HashMap<String, List<String[]>>();
String key = "";
List<String[]> positionList;
String pos[];
Cursor organization = resolver.query(Data.CONTENT_URI, null,
Data.CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE
+ "='" + Organization.CONTENT_ITEM_TYPE + "'", null,
null);
while (organization.moveToNext()) {
pos = new String[2];
key = organization.getString(organization
.getColumnIndex(CommonDataKinds.Organization.DATA2));
pos[0] = organization.getString(organization
.getColumnIndex(CommonDataKinds.Organization.COMPANY));
pos[1] = organization.getString(organization
.getColumnIndex(CommonDataKinds.Organization.TITLE));
positionList = companyMap.get(key);
if(positionList!=null&&!positionList.isEmpty()){
positionList.add(pos);
companyMap.put(key, positionList);
}else{
positionList = new ArrayList<String[]>();
positionList.add(pos);
companyMap.put(key, positionList);
}
// positionList.add(pos);
System.out.println("--------------------------------");
System.out.println("type:" + key);
System.out.println("company:" + pos[0]);
System.out.println("position:" + pos[1]);
}
return companyMap;
}
//=========================================List<String> 数据类型===========================================
/**
* 根据用户id 获得notes
*/
public static List<String> getNotes(String contactId) {
List<String> notesList = new ArrayList<String>();
String note = "";
Cursor notes = resolver.query(Data.CONTENT_URI, null,
Data.CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE
+ "='" + Note.CONTENT_ITEM_TYPE + "'",
null, null);
while (notes.moveToNext()) {
note = notes
.getString(notes
.getColumnIndex(Note.NOTE));
System.out.println("note :" + note);
notesList.add(note);
}
return notesList;
}
/**
* 根据用户id 获得nicks
*/
public static List<String> getNick(String contactId) {
List<String> nickList = new ArrayList<String>();
String nick = "";
Cursor nicks = resolver.query(Data.CONTENT_URI, null,
Data.CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE
+ "='" + Nickname.CONTENT_ITEM_TYPE + "'",
null, null);
while (nicks.moveToNext()) {
nick = nicks
.getString(nicks
.getColumnIndex(Nickname.DATA1));
System.out.println("nick :" + nick);
nickList.add(nick);
}
return nickList;
}
/**
* 根据用户id 获得website
*/
public static List<String> getWebsite(String contactId) {
List<String> webSiteList = new ArrayList<String>();
String webSite = "";
Cursor nicks = resolver.query(Data.CONTENT_URI, null,
Data.CONTACT_ID + "=" + contactId + " AND " + Data.MIMETYPE
+ "='" + Website.CONTENT_ITEM_TYPE + "'",
null, null);
while (nicks.moveToNext()) {
webSite = nicks
.getString(nicks
.getColumnIndex(Website.DATA1));
webSiteList.add(webSite);
System.out.println("webSite : "+ webSite);
}
return webSiteList;
}
}