2

I'm trying to learn how to make an autocomplete textview in an android app would have a dropdown list of contacts with their numbers, based on the letters that the user enters that match a part of a name of a contact.

How would I accomplish this? I've spend hours on google without finding a complete solution, which I thought was odd.

Any help would be appreciated!

Thanks,

A beginner android developer

MORE DETAILS: I wish to have a textbox similar to the recipient box for the default messaging app, where I would be able to start typing the name of a contact, and suggestions of contact names with their numbers would appear, which I would be able to click and fill my textbox with the number of that contact that I clicked.

PuffySparrow
  • 897
  • 3
  • 10
  • 23
  • I have found my solution here: http://stackoverflow.com/questions/12400504/selecting-contact-from-autocomplete-textview?rq=1 – PuffySparrow Jan 26 '14 at 21:56

3 Answers3

2

You can try with this one:

Modify how you want to display the UI

// I just hard coded the number retrive the number and append.

public class MainActivity extends Activity implements TextWatcher {

String[] arrcontact = null;
AutoCompleteTextView myAutoComplete;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myAutoComplete = (AutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);

    loadContact();

    myAutoComplete.addTextChangedListener(this);
    myAutoComplete.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, arrcontact));
}

private void loadContact() {
    Cursor cursor = getContacts();
    arrcontact = new String[30];

    int count = 0;

    while (cursor.moveToNext()) {
        String displayName = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));


        arrcontact[count] = displayName + "\n" + "908228282";
        count++;
        if (count == 30)
            break;

    }
}

private Cursor getContacts() {
    // Run query
    final ContentResolver cr = getContentResolver();
    String[] projection = { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts._ID };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?";
    String[] selectionArgs = { "1" };
    final Cursor contacts = cr.query(ContactsContract.Contacts.CONTENT_URI,
            projection, selection, selectionArgs, "UPPER("
                    + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
    return contacts;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

}

kedark
  • 244
  • 1
  • 7
1

Hi could this solve what you need?

http://developer.android.com/guide/topics/ui/controls/text.html#AutoComplete

As long as you have your contact lists in a StringArray, it would allow you to use this API.

====================================

According to your added comment, it looks like you also need what this post did in grabContacts() with ContentResolver and query, and then creating an StringArray from the Cursor as presented in the post:

Android - Autocomplete with contacts

(kedark's solution is good and complete, though this particular method could be a bit more easy to understand for beginners)

private Cursor grabContacts(){
    // Form an array specifying which columns to return. 
    String[] projection = new String[] {People._ID, People.NAME};

    // Get the base URI for the People table in the Contacts content provider.
    Uri contacts =  People.CONTENT_URI;

    // Make the query. 
    Cursor managedCursor = managedQuery(contacts, projection, null, null, People.NAME + " ASC"); // Put the results in ascending order by name
    startManagingCursor(managedCursor);
    return managedCursor;
}

You can upvote them if this helps :)

Community
  • 1
  • 1
Jim
  • 36
  • 3
  • Thanks, but not quite. I wish to have a textbox similar to the recipient box for the default messaging app, where I would be able to start typing the name of a contact, and suggestions of contact names with their numbers would appear, which I would be able to click and fill my textbox with the number of that contact that I clicked. – PuffySparrow Jan 22 '14 at 04:09
0

I updated Kedark's loadContact() routine to work for any length contact list (below) otherwise it works well. Thanks!

    private String[] loadContactList() {
        String[] contactArray;
        Cursor cursor = getContacts();

        contactArray = new String[cursor.getCount()];

        int count = 0;

        while (cursor.moveToNext()) {
            String displayName = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
            contactArray[count] = displayName;
            count++;
        }
        cursor.close();
        return(contactArray);
    }
Amy McBlane
  • 164
  • 3