-1

I want to display the saved contacts in android emulator to my application.the contacts list should display like first name,last name etc.i.e. i want to create separate lists for all fields.please help me.Thanks in advance.

MJBoss
  • 55
  • 1
  • 7
  • possible duplicate of [How to call Android contacts list?](http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list) – Paresh Mayani Jul 05 '11 at 07:04
  • [similar question and answer here](http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list) – Pratik Jul 05 '11 at 06:48

1 Answers1

0

Hi try the code given below. It wil fetch u the contacts available in the emulator, U can then create a listview to show the available contacts.

people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int position=0;
Cursor q=db.query(mProfile,new String[]{"person_name"},"person_name"+"!='"+null+"'",null,null, null, null);
people.moveToFirst();
int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);    
    while(!people.isAfterLast()) {
        Cursor o=db.query(mProfile,new String[]{"person_name"},"person_name"+"='"+people.getString(nameFieldColumnIndex)+"'",null,null, null, null);
        if(!(o.getCount()>0))
        {  
        mConname.add(position, people.getString(nameFieldColumnIndex));
        try{
            String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
            String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            if ( hasPhone.equalsIgnoreCase("1"))
                hasPhone = "true";
            else
                hasPhone = "false" ;
            if (Boolean.parseBoolean(hasPhone)) 
            {
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                while (phones.moveToNext()) 
                {
                    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    mConno.add(position,phoneNumber);

                }
                phones.close(); 
            }   
            if(hasPhone=="false")
            {   mConname.remove(position);
            }   
            else
                position++;
        }       
        catch(Exception e)
        { 

        }
    }
        people.moveToNext();
    }

Also go thro' this tutorial (Working with Android Contacts) u can have a clear idea.. Also look this tutorial(query-contacts-in-android)

Community
  • 1
  • 1
Hussain
  • 5,552
  • 4
  • 40
  • 50