日期:2014-05-16  浏览次数:20533 次

Android联系人数据库全解析(3)
Working With Android Contacts
轻松玩转Android联系人
Gluing it together
把两个版本结合起来
To put this together into an application there are a few glue pieces that need to
be setup along with creating classes to manage accessing the data. First we need to create a set of classes to hold the data. Also we'll create a class to handle 2.0 API calls and a class to handle 1.6 and earlier API calls. There's also a wrapper class that determines and loads the proper class.
把两个版本的联系人操作整合起来,我们需要建立一些读数据的类。首先,我们需要建立一些存储数据的类,我们还需要一个类来分别对2.0的API和1.6以前的API进行处理的类。我们还必须建立一个解析类然后去加载合适的类。
Contact Data Classes
联系人数据类
The contact classes are a series of classes to hold a list of contacts. The list is stored in the class ContactList that maintains an ArrayList of Contacts. The Contact objects are represented in the Contact class. The contact class stores all the data from the Android contact record. In addition to the ContactList and Contact classes there are specialized classes to represent some of the record data.
联系人类是一系列抽象了联系人列表的类。这个ContactList中列表保存了联系人的列表。联系人对象被封装在联系人类中,联系人类存储了Android系统联系人记录中的所有数据。除了ContactList和Contact类之外,还有一些特殊的类来封装其他的记录数据。
We will create classes to represent the address, email, instant messenger, phone number, and organization(s). Most of these classes are mere data storage classes with variables and getter/setters.
我们将建立类来封装“地址”、“电子邮件”、“实时消息”、“电话号码”、和“分组”。这些类中大多数都仅仅有一些属性和一些getter/setter方法。
ContactList

The ContactList class is a very basic class designed to hold an ArrayList of instances of the Contact class below. We've left this class very plain and ready to be expanded to suit your needs.
package com.higherpass.android.ContactAPI.objects;

import java.util.ArrayList;

public class ContactList {

private ArrayList<Contact> contacts = new ArrayList<Contact>();

public ArrayList<Contact> getContacts() {
return contacts;
}

public void setContacts(ArrayList<Contact> contacts) {
this.contacts = contacts;
}

public void addContact(Contact contact) {
this.contacts.add(contact);
}
 
public ContactList() {

}

}
Contact

The Contact class is used to store the details about each contact. There are a series of private class variables to hold this data. Singular data such as name and database ID are stored as strings. Complex data is stored either as an instance or ArrayList of data specific classes. This class is mainly getters and setters with a few methods to add to the internal ArrayLists.
package com.higherpass.android.ContactAPI.objects;

import java.util.ArrayList;

public class Contact {
private String id;
private String displayName;
private ArrayList<Phone> phone;
private ArrayList<Email> email;
private ArrayList<String> notes;
private ArrayList<Address> addresses = new ArrayList<Address>();
private ArrayList<IM> imAddresses;
private Organization organization;
 

public Organization getOrganization() {
return organization;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
public ArrayList<IM> getImAddresses() {
return imAddresses;
}
public void setImAddresses(ArrayList<IM> imAddresses) {
this.imAddresses = imAddresses;
  }
public void addImAddresses(IM imAddr) {
this.imAddresses.add(imAddr);
}
public ArrayList<String> getNotes() {
return notes;
}
public void setNotes(ArrayList<String> notes) {
this.notes = notes;
}
public void addNote(String note) {