开发者

How to pick contact number using autocomplete in android?

I have implemented an autocomplete feature in my application, but it picks up only the name of the contact instead of the number. Whenever i select a field in the autocomplete textbox, the number should be picked from the phone's contact list and placed in the text box...please help me out! :)

public class AutoMultipleContacts extends Activity {

private static final int PICK_CONTACT = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.multipleselect);

    Cursor peopleCursor =   getContentResolver().query(Contacts.People.CONTENT_URI, PEOPLE_PROJECTION, null, null, Contacts.People.DEFAULT_SORT_ORDER);
    ContactListAdapter contactadapter = new ContactListAdapter(this,peopleCursor);
    MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.contacts);
    textView.setAdapter(contactadapter);
    textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}


public static class ContactListAdapter extends CursorAdapter implements Filterable {
    public ContactListAdapter(Context context, Cursor c) {
        super(context, c);
        mContent = context.getContentResolver();
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        final TextView view = (TextView) inflater.inflate(
                android.R.layout.simple_dropdown_item_1line, parent, false);
        view.setText(cursor.getString(5));
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ((TextView) view).setText(cursor.getString(5));
    }

    @Override
    public String convertToString(Cursor cursor) {
        return cursor.getString(5);
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (getFilterQueryProvider() != null) {
            return getFilterQueryProvider().runQuery(constraint);
        }

        StringBuilder buffer = null;
        String[] args = null;
        if (constraint != null) {
            buffer = new StringBuilder();
            buffer.append("UPPER(");
            buffer.append(Contacts.Con开发者_开发技巧tactMethods.NAME);
            buffer.append(") GLOB ?");
            args = new String[] { constraint.toString().toUpperCase() + "*" };
        }

        return mContent.query(Contacts.People.CONTENT_URI, PEOPLE_PROJECTION,
                buffer == null ? null : buffer.toString(), args,
                Contacts.People.DEFAULT_SORT_ORDER);
    }

    private ContentResolver mContent;        
}

private static final String[] PEOPLE_PROJECTION = new String[] {
    Contacts.People._ID,
    Contacts.People.PRIMARY_PHONE_ID,
    Contacts.People.TYPE,
    Contacts.People.NUMBER,
    Contacts.People.LABEL,
    Contacts.People.NAME,
};

}


Try changing

@Override
public String convertToString(Cursor cursor) {
    return cursor.getString(5);
}

To

@Override
public String convertToString(Cursor cursor) {
    return cursor.getString(3);
} 


Append PhoneNumber to the buffer instead of Name and return cursor.getString(3) and setText(cursor.getString(3)). Also People is deprecated now. Use ContactsContracts instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜