i am getting the phone no,mail, name from contacts and stored in side the 3array list, display the contacts.
the contacts are stored in side the array list sequentially.
if i store a contact contains phone no,name and with out mail id.
i want to store the mail id null in side the mails array list.
if any one has solution please help me.
Thanks in advance.
Main Activity how can retrieve data...开发者_开发知识库
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.......................................
}
}
Try with the following approach
Try to create a bean where you will store details and create an arraylist of that bean class
AddressBook.java
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;
}
}
here Class B does not extends Activity. But write the following method to gwt contacts and email id in class B
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();
}
Write two bean class
EmailBean
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;
}
}
ContactNumberBean
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;
}
}
Thanks Deepak
精彩评论