Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7868293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:57:14+00:00 2026-06-03T00:57:14+00:00

I am trying to implement a custom AutoCompleteTextView for choosing a contact’s phone number

  • 0

I am trying to implement a custom AutoCompleteTextView for choosing a contact’s phone number from a list of suggestions that display the contact name, phone number type, and phone number. I created a custom CursorAdapter that defines and sets my Layout and TextViews for each suggestion and queries contacts based on the user-entered text via runQueryOnBackgroundThread. I’m running into an issue where the suggestions seem correct for the first two values entered (e.g. “ab” suggests “abcd” and “abyz”) but not for anything beyond that (e.g. “abc” suggests “abyz”). For the latter, when the “abyz” suggestion is selected, the values for “abcd” are returned.

Code for the main activity:

final ContactInfo cont = new ContactInfo(ctx);
    Cursor contacts = cont.getContacts2(null);
    startManagingCursor(contacts);

    ContactsAutoCompleteCursorAdapter adapter = new ContactsAutoCompleteCursorAdapter(this, contacts);
    mPersonText.setAdapter(adapter);
    mPersonText.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
            String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
            mPersonNum.setText(number);
        }
    });

Code for my contacts class that returns a cursor for all contacts:

public Cursor getContacts2(String where)
{
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.TYPE,
            ContactsContract.CommonDataKinds.Phone.NUMBER};

    Cursor people = ctx.getContentResolver().query(uri, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

    return people;
}

Code for my CursorAdapter:

public class ContactsAutoCompleteCursorAdapter extends CursorAdapter implements Filterable {

private TextView mName, mType, mNumber;
private ContentResolver mContent;

public ContactsAutoCompleteCursorAdapter(Context context, Cursor c) {
    super(context, c);
    mContent = context.getContentResolver();
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    final LayoutInflater mInflater = LayoutInflater.from(context);
    final View ret = mInflater.inflate(R.layout.contacts_auto_list, null);

    mName = (TextView) ret.findViewById(R.id.name);
    mType = (TextView) ret.findViewById(R.id.phonetype);
    mNumber = (TextView) ret.findViewById(R.id.phonenum);

    return ret;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

    String name = cursor.getString(nameIdx);
    int type = cursor.getInt(typeIdx);
    String number = cursor.getString(numberIdx);

    mName.setText(name);
    if (type == 1) {mType.setText("Home");}
    else if (type == 2) {mType.setText("Mobile");}
    else if (type == 3) {mType.setText("Work");}
    else {mType.setText("Other");}
    mNumber.setText(number);

}

@Override
public String convertToString(Cursor cursor) {
    int nameCol = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    String name = cursor.getString(nameCol);
    return name;
}

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    // this is how you query for suggestions
    // notice it is just a StringBuilder building the WHERE clause of a cursor which is the used to query for results
    if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }

    String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Phone._ID,
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.TYPE,
            ContactsContract.CommonDataKinds.Phone.NUMBER};

    return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, 
            "UPPER(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") LIKE '" + constraint.toString().toUpperCase() + "%'", null, 
            ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}

}

As I said above, when the user enters “ab” into the AutoCompleteTextView the suggestions are “abcd” and “abyz”, however when the user types “abc” the suggestion is just “abyz”. When the user selects “abyz” in that case, the values for “abcd” are returned. Here are two screenshots that show what I’m trying to describe:

enter image description hereenter image description here

I’ve read every question I could find here and elsewhere but can’t seem to figure this out. I’m fairly new to Android development so I apologize in advance if my mistake is a simple one. Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:57:16+00:00Added an answer on June 3, 2026 at 12:57 am

    I seem to have answered my own question after more research. Moving the setting of the views for my textViews from the newView function to the bindView function seems to have done the trick, which I think makes sense…

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
    
        final LayoutInflater mInflater = LayoutInflater.from(context);
        final View ret = mInflater.inflate(R.layout.contacts_auto_list, null);
    
        return ret;
    }
    
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
    
        int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
        int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    
        String name = cursor.getString(nameIdx);
        int type = cursor.getInt(typeIdx);
        String number = cursor.getString(numberIdx);
    
        mName = (TextView) view.findViewById(R.id.name);
        mType = (TextView) view.findViewById(R.id.phonetype);
        mNumber = (TextView) view.findViewById(R.id.phonenum);
    
        mName.setText(name);
        if (type == 1) {mType.setText("Home");}
        else if (type == 2) {mType.setText("Mobile");}
        else if (type == 3) {mType.setText("Work");}
        else {mType.setText("Other");}
        mNumber.setText(number);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a custom class for dataannotations that retrieves error messages from
I am trying to implement a custom validator for phone number on an ASP.NET
I'm trying to implement a custom validation annotation in Seam. We have a list
I'm trying to implement a custom log4net logger/logmanager so that I can add a
I'm trying to implement a custom combobox options popup, so that near each option
I'm trying to implement custom suggestions for qsb. I have added CREATE TABLE IF
I'm trying to implement a custom sort field for a list of records. When
I am trying to implement a custom profile class in C# that inherits System.Web.Profile.ProfileBase.
I'm trying to implement to open a custom dialog box having related info from
I'm trying to implement a custom panel control that would act as a naming

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.