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;
}

}

更多相关文章

  1. Android(安卓)cursor query方法详解
  2. 关于Android中的数据存储
  3. Android(安卓)API 中文 (50) —— SpinnerAdapter
  4. Android(安卓)数据序列化总结
  5. 【Android(安卓)Api 翻译4】android api 完整翻译之Contacts Pro
  6. Android数据库操作查询中Cursor类的问题
  7. Android(安卓)tips2
  8. Android(安卓)- Intent基础
  9. mybatisplus的坑 insert标签insert into select无参数问题的解决

随机推荐

  1. Android使用系统文件管理器选择文件,并将U
  2. Android官方开发中心之Web Apps屏幕适配
  3. android 评分设置显示小星星
  4. android存放本地数据
  5. 【Android】源码分析 - LRUCache缓存实现
  6. 『叫兽学堂』在Android SDK 1.6上抢先体
  7. Android之——激活应用程序的详情界面
  8. 2018Android面试知识架构整理(转)
  9. Android 关于AIDL通信,RemoteCallbackList
  10. Flutter修仙传第一章:从Form入手学会组件