0

I am working an android project and I am trying to make use of the the AutoCompleteEditText and bind the ArrayAdapter of the contacts. But I am having an issue returning an array of contacts.

Below is the code I am using to retrieve the contacts and display name.

public ArrayList<String> getPhoneNumbersAndContactNames()
    {
        ArrayList<String> contacts = new ArrayList<String>();
        try
        {
            ContentResolver contentResolver = context.getContentResolver();

            Uri uri = Data.CONTENT_URI;
            String[] projection = new String[] {PhoneLookup._ID, PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER};
            String selection = "*";
            Cursor cursor = contentResolver.query(uri, projection, selection, null, null);
            if (cursor.moveToFirst())
            {
                contacts.add(cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME)));
            }
        }
        catch (SQLiteException ex)
        {
            Log.d("Database All Contacts Exception", ex.toString());
        }
        catch (Exception ex)
        {
            Log.d("Get All Contacts Exception", ex.toString());
        }
        return contacts;
    }

In the code above I am getting the exception:

IllegalArgumentException: Invalid column number

Below is how I am trying to bind the array list to the auto complete edit text.

txtPhoneNumberOrContact = (AutoCompleteTextView)findViewById(R.id.call_blacklist_txtPhoneNumberContactName);

ArrayList<String> contacts = common.getPhoneNumbersAndContactNames();

ArrayAdapter<String> contactAdapter = 
        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, 
                contacts);

txtPhoneNumberOrContact.setAdapter(contactAdapter);

I can't figure out why I am getting the exception when trying to retrieve the list of contacts to add to the ArrayList.

Thanks for any help you can provide.

Boardy
  • 35,417
  • 104
  • 256
  • 447

1 Answers1

0

Use other selection not "*" but: " = ?"

In my project I used this flow that has been worked for me:

Map<String, String> contactDataMap = new HashMap<String, String>();

    ContentResolver cr = context.getContentResolver();

    Uri contactData = data.getData();       
    Cursor cursor =  cr.query(contactData, null, null, null, null);
    cursor.moveToFirst();
    String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
    String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

    contactDataMap.put(NAME, (name != null)?name:"");


    if (Integer.parseInt(cursor.getString(
            cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
        Cursor pCur = cr.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                null, 
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                new String[]{id},
                                null);

        while (pCur.moveToNext()) {
            String number = pCur.getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contactDataMap.put(PHONE, (number != null)?number:"");
            break; // ? we want only 1 value
        } 
        pCur.close();
    }

Here you fill contactDataMap with names as key and phone number

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225