这是我在arraylist中存储电子邮件地址和联系人姓名的代码
public ArrayList<ContactVo> getNameEmailDetails() {
ArrayList<ContactVo> m_arrList = new ArrayList<ContactVo>();
ArrayList<String> emlRecs = new ArrayList<String>();
HashSet<String> emlRecsHS = new HashSet<String>();
ContentResolver cr = m_context.getContentResolver();
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String order = "CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME + ", "
+ ContactsContract.CommonDataKinds.Email.DATA
+ " COLLATE NOCASE";
String filter = ContactsContract.CommonDataKinds.Email.DATA
+ " NOT LIKE ''";
Cursor cur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION,
filter, null, order);
ContactVo m_vo;
if (cur.moveToFirst()) {
do {
m_vo = new ContactVo();
// names comes in hand sometimes
String name = cur.getString(1);
String emlAddr = cur.getString(3);
System.err.println("Value Is---------->" + cur.getString(2)
+ "Name--" + name + "Email---" + emlAddr);
m_vo.setM_sName(name);
m_vo.setM_sEmail(emlAddr);
// keep unique only
if (emlRecsHS.add(emlAddr.toLowerCase())) {
emlRecs.add(emlAddr);
}
m_arrList.add(m_vo);
} while (cur.moveToNext());
}
cur.close();
return m_arrList;
}
编辑:
在显示所有联系人列表的列表视图的 onItemClick 事件中编写以下代码。
m_lvList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(m_context, m_arrContact.get(arg2).getM_sEmail(), Toast.LENGTH_LONG).show();
Intent in = new Intent(m_context, Test.class);
in.putExtra("pos", arg2);
in.putParcelableArrayListExtra("parcel", m_arrContact);
startActivity(in);
}
});
让您的 vo parcelable 将详细信息传递到另一个活动并访问它。
联系方式
public class ContactVo implements Parcelable {
private String m_sName, m_sEmail;
public ContactVo() {
}
public ContactVo(Parcel in) {
readFromParcel(in);
}
public String getM_sName() {
return m_sName;
}
public void setM_sName(String m_sName) {
this.m_sName = m_sName;
}
public String getM_sEmail() {
return m_sEmail;
}
public void setM_sEmail(String m_sEmail) {
this.m_sEmail = m_sEmail;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(m_sName);
dest.writeString(m_sEmail);
}
public void readFromParcel(Parcel p_in) {
m_sName = p_in.readString();
m_sEmail = p_in.readString();
}
public static final Parcelable.Creator<ContactVo> CREATOR = new Parcelable.Creator<ContactVo>() {
public ContactVo createFromParcel(Parcel s) {
return new ContactVo(s);
}
public ContactVo[] newArray(int size) {
return new ContactVo[size];
}
};
}
在您的另一项活动中,如下所示:
public class Test extends Activity {
ArrayList<ContactVo> m_arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
m_arr = getIntent().getParcelableArrayListExtra("parcel");
int pos = getIntent().getIntExtra("pos", 0);
System.out.println("Detailsss------>" + m_arr.get(pos).getM_sEmail()
+ m_arr.get(pos).getM_sName());
}
}
您可以在其中添加任意数量的详细信息并将其ArrayList<ContactVo>
传递给另一个活动。
要获取其他联系方式,请查看这里