1

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.......................................
    }
    }
kiran
  • 3,244
  • 7
  • 34
  • 57
  • What's the problem? There's no rule against adding null objects to a list. – trutheality Jun 16 '11 at 05:07
  • [one](http://stackoverflow.com/questions/4192559/is-it-possible-to-selecte-multiple-contacts-from-contact-picker), [two](http://stackoverflow.com/questions/3726282/how-can-i-add-a-contact-in-android) and [three](http://stackoverflow.com/questions/4387960/contact-api-storing-contact-as-a-invisible-contact-how-to-make-it-visible) hope these link may be helpful – Ads Jun 16 '11 at 05:12

1 Answers1

1

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

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • did you try with the above approach? if yes what is the problem can you ellaborate so that i will try to help you because I have tested the above approach – Sunil Kumar Sahoo Jun 16 '11 at 06:02
  • how can retrieve data in main activity please forward some solution Class A extends Activity{ new ClassB(this);//how can implemented retrieve list functionality... } – kiran Jun 16 '11 at 06:10
  • i am edit my question adding main activity plaese see once deepak i am struck in more days – kiran Jun 16 '11 at 06:17
  • check my answer now. It wil work for you as I have done with implementation. If any issue come let me know – Sunil Kumar Sahoo Jun 16 '11 at 06:32
  • you can make a upvote or accept if my answer is working for you – Sunil Kumar Sahoo Jun 16 '11 at 07:03
  • my problem is other contact details pass to class to activity i am getting contacts in ContactList1 class in main activity create instance of the class how get contact list – kiran Jun 16 '11 at 07:31