get All Contacts As String List - Android Account

Android examples for Account:Contact Get

Description

get All Contacts As String List

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

public class Main {
    public static List<String> getAllContactsAsStringList(
            Activity callingActivity) {//from w w w. j  a  v  a2s  . co  m
        List<String> allContactsStringList = new ArrayList<String>();
        Cursor phones = callingActivity.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null, null);
        while (phones.moveToNext()) {
            String contact = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            allContactsStringList.add(contact + "<" + number + ">");
            Log.i("ReadContacts", contact + "<" + number + ">");
        }
        return allContactsStringList;
    }
}

Related Tutorials