Getting number and names of contacts in android

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

i am getting all contact names and number bur i just want to show a number and a name which i choose.

here is the code which return all contacts :

ArrayList<String> contactList = new ArrayList<String>();

    switch (reqCode) {
    case (0):
        if (resultCode == Activity.RESULT_OK) {

            Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] projection = new String[] {
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER };

            Cursor people = getContentResolver().query(uri, projection,
                    null, null, null);

            int indexName = people
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int indexNumber = people
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            people.moveToFirst();
            do {
                String name = people.getString(indexName);
                String number = people.getString(indexNumber);

                String contact = name + "" + number;
                contactList.add(contact);
            } while (people.moveToNext());

        }

in contactList i want to add a number and a name

Answers

As I understand your question you want add number and name in collection-Use HashMap for that.

 HashMap<String,String> contactList = new HashMap<String,String>();

switch (reqCode) {
case (0):
    if (resultCode == Activity.RESULT_OK) {

        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };

        Cursor people = getContentResolver().query(uri, projection,
                null, null, null);

        int indexName = people
                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = people
                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        people.moveToFirst();
        do {
            String name = people.getString(indexName);
            String number = people.getString(indexNumber);

            String contact = name + "" + number;
            contactList.put(number,name);
        } while (people.moveToNext());

    }

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/18201108/getting-number-and-names-of-contacts-in-android

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils