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) {
this.notes.add(note);
}
public ArrayList<Address> getAddresses() {
return addresses;
}
public void setAddresses(ArrayList<Address> addresses) {
this.addresses = addresses;
}
public void addAddress(Address address) {
this.addresses.add(address);
}
public ArrayList<Email> getEmail() {
return email;
}
public void setEmail(ArrayList<Email> email) {
this.email = email;
}
public void addEmail(Email e) {
this.email.add(e);
}
public String getId() {
return id;
}
public void setId(String id) {
  this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String dName) {
this.displayName = dName;
}
public ArrayList<Phone> getPhone() {
return phone;
}
public void setPhone(ArrayList<Phone> phone) {
this.phone = phone;
}
public void addPhone(Phone phone) {
this.phone.add(phone);
}
}
Address

The Address class is the only class in the ContactList framework that actually does any work. Due to differences in data storage between 1.x and 2.0 versions of Android the Address class has to determine if the input address is free-form from 1.x or was input from the formatted data structure from 2.0. Android 2.0 has individual data columns for PO-Box, street, city, region, postal code, country while 1.x was just a text field with the entire address. This is handled in the toString() method that returns a free-form address from either input type. Unfortunately when using Android 1.x all the individual address getters will return null.
package com.higherpass.android.ContactAPI.objects;

public class Address {
private String poBox;
private String street;
private String city;
private String state;
private String postalCode;
private String country;
private String type;
private String asString = "";

public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPoBox() {
return poBox;
}
public void setPoBox(String poBox) {
this.poBox = poBox;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String toString() {
if (this.asString.length() > 0) {
return(this.asString);
} else {
String addr = "";
if (this.getPoBox() != null) {
addr = addr + this.getPoBox() + "n";
}
if (this.getStreet() != null) {
addr = addr + this.getStreet() + "n";
}
if (this.getCity() != null) {
addr = addr + this.getCity() + ", ";
}
if (this.getState() != null) {
addr = addr + this.getState() + " ";
}
if (this.getPostalCode() != null) {
addr = addr + this.getPostalCode() + " ";
}
if (this.getCountry() != null) {
addr = addr + this.getCountry();
}
return(addr);
}
}

public Address(String asString, String type) {
this.asString = asString;
this.type = type;
}

public Address(String poBox, String street, String city, String state,
String postal, String country, String type) {
this.setPoBox(poBox);
  this.setStreet(street);
this.setCity(city);
this.setState(state);
this.setPostalCode(postal);
this.setCountry(country);
this.setType(type);
}
}

Email

Another getter/setter and data storage class. The email class stores the email address and address type (work, home, etc).
package com.higherpass.android.ContactAPI.objects;

public class Email {
private String address;
private String type;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getType() {
return type;
}
public void setType(String t) {
this.type = t;
}

public Email(String a, String t) {
this.address = a;
this.type = t;
}
}
IM

Class to hold instant messenger data.
package com.higherpass.android.ContactAPI.objects;

public class IM {
private String name;
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

public IM(String name, String type) {
this.name = name;
this.type = type;
}
}
Organization

Class to hold the contacts organizational data.
package com.higherpass.android.ContactAPI.objects;

public class Organization {
private String organization = "";
private String title = "";
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}

public Organization() {

}
public Organization(String org, String title) {
this.organization = org;
this.title = title;
}
}
Phone

Class to hold the phone records.
package com.higherpass.android.ContactAPI.objects;

public class Phone {
private String number;
private String type;

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Phone(String n, String t) {
this.number = n;
this.type = t;
}

}

你可能感兴趣的:(android)