我从联系人处获取电话号码,邮件,姓名并存储在3array列表中,显示联系人。
联系人按顺序存储在数组列表的一侧。
如果我存储的联系人包含电话号码,姓名和邮件ID。
我想将邮件id null存储在邮件数组列表的旁边。
如果有人有解决方案,请帮助我。
提前致谢。
主要活动如何检索数据...
public class AddressBook extends Activity
{
ListView lv1;
String[] row,row1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//how can retrieve contact details ...................................................
ContactList1.getContactEmails(this,emailContacts1);
ContactList1.getContactNumbers(this,phoneContacts1);
how can implemented the code.......................................
}
}
答案 0 :(得分:1)
尝试以下方法
尝试创建一个bean,您将在其中存储详细信息并创建该bean类的arraylist
import java.util.ArrayList;
import java.util.Iterator;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
public class AddressBook extends Activity
{
ListView lv1;
String[] row,row1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<ContactEmailBean> emailContacts1 = getContactEmails(this);
ArrayList<ContactNumberBean> phoneContacts1 = getContactNumbers(this);
}
public ArrayList<ContactNumberBean> getContactNumbers(Context context) {
String contactNumber = null;
int contactNumberType = Phone.TYPE_MOBILE;
String nameOfContact = null;
ArrayList<ContactNumberBean> phoneContacts = new ArrayList<ContactNumberBean>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(BaseColumns._ID));
nameOfContact = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id },
null);
while (phones.moveToNext()) {
contactNumber = phones.getString(phones
.getColumnIndex(Phone.NUMBER));
contactNumberType = phones.getInt(phones
.getColumnIndex(Phone.TYPE));
phoneContacts
.add(new ContactNumberBean(nameOfContact,
contactNumber, contactNumberType));
}
phones.close();
}
}
}// end of contact name cursor
cur.close();
return phoneContacts;
}
/**
*
* This method is responsible to get native contacts and corresponding email
* id (ApplicationConstants.emailContacts)
*
* @param context
*/
public ArrayList<ContactEmailBean> getContactEmails(Context context) {
String emailIdOfContact = null;
int emailType = Email.TYPE_WORK;
String contactName = null;
ArrayList<ContactEmailBean> emailContacts = new ArrayList<ContactEmailBean>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(BaseColumns._ID));
contactName = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Log.i(TAG,"....contact name....." +
// contactName);
cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
Cursor emails = cr.query(Email.CONTENT_URI, null,
Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext()) {
emailIdOfContact = emails.getString(emails
.getColumnIndex(Email.DATA));
// Log.i(TAG,"...COntact Name ...."
// + contactName + "...contact Number..."
// + emailIdOfContact);
emailType = emails.getInt(emails
.getColumnIndex(Phone.TYPE));
emailContacts
.add(new ContactEmailBean(contactName,
emailIdOfContact, emailType));
}
emails.close();
}
}// end of contact name cursor
cur.close();
return emailContacts;
}
}
此处B类不扩展活动。 但是将以下方法写入gwt联系人和B类中的电子邮件ID
public static void getContactNumbers(Context context) {
String contactNumber = null;
int contactNumberType = Phone.TYPE_MOBILE;
String nameOfContact = null;
ArrayList<ContactNumberBean> phoneContacts = new ArrayList<ContactNumberBean>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(BaseColumns._ID));
nameOfContact = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id },
null);
while (phones.moveToNext()) {
contactNumber = phones.getString(phones
.getColumnIndex(Phone.NUMBER));
contactNumberType = phones.getInt(phones
.getColumnIndex(Phone.TYPE));
phoneContacts
.add(new ContactNumberBean(nameOfContact,
contactNumber, contactNumberType));
}
phones.close();
}
}
}// end of contact name cursor
cur.close();
}
/**
*
* This method is responsible to get native contacts and corresponding email
* id (ApplicationConstants.emailContacts)
*
* @param context
*/
public static void getContactEmails(Context context) {
String emailIdOfContact = null;
int emailType = Email.TYPE_WORK;
String contactName = null;
ArrayList<ContactEmailBean> emailContacts = new ArrayList<ContactEmailBean>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(BaseColumns._ID));
contactName = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Log.i(TAG,"....contact name....." +
// contactName);
cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
Cursor emails = cr.query(Email.CONTENT_URI, null,
Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext()) {
emailIdOfContact = emails.getString(emails
.getColumnIndex(Email.DATA));
// Log.i(TAG,"...COntact Name ...."
// + contactName + "...contact Number..."
// + emailIdOfContact);
emailType = emails.getInt(emails
.getColumnIndex(Phone.TYPE));
emailContacts
.add(new ContactEmailBean(contactName,
emailIdOfContact, emailType));
}
emails.close();
}
}// end of contact name cursor
cur.close();
}
写两个bean类
public class ContactEmailBean {
String emailType = null;
String nameOfContact = null;
String emailIdOfContact = null;
public ContactEmailBean(String nameOfContact, String emailIdOfContact,
int emailType) {
switch (emailType) {
case Email.TYPE_HOME:
this.emailType = "HOME";
// do something with the Home number here...
break;
case Email.TYPE_MOBILE:
this.emailType = "MOBILE";
// do something with the Mobile number here...
break;
case Email.TYPE_WORK:
this.emailType = "WORK";
// do something with the Work number here...
break;
default:
this.emailType = "OTHER";
break;
}
this.nameOfContact = nameOfContact;
this.emailIdOfContact = emailIdOfContact;
}
public String getNameOfContact() {
return this.nameOfContact;
}
public String getEmailType() {
return this.emailType;
}
public String getEmailIdOfContact() {
return this.emailIdOfContact;
}
}
public class ContactNumberBean {
String phoneNumberType = null;
String nameOfContact = null;
String contactNumber = null;
public ContactNumberBean(String nameOfContact, String contactNumber,
int contactNumberType) {
switch (contactNumberType) {
case Phone.TYPE_HOME:
this.phoneNumberType = "HOME";
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
this.phoneNumberType = "MOBILE";
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
this.phoneNumberType = "WORK";
// do something with the Work number here...
break;
case Phone.TYPE_WORK_MOBILE:
this.phoneNumberType = "WORK";
break;
case Phone.TYPE_FAX_HOME:
this.phoneNumberType = "FAX";
break;
default:
this.phoneNumberType = "OTHER";
break;
}
this.nameOfContact = nameOfContact;
this.contactNumber = contactNumber;
}
public String getNameOfContact() {
return this.nameOfContact;
}
public String getPhoneNumberType() {
return this.phoneNumberType;
}
public String getContactNumber() {
return this.contactNumber;
}
}
由于 迪帕克