Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

android - MultiAutoCompleteTextView with contacts phone numbers

I need to create a MultiAutoCompleteTextView with the phone numbers of the contacts on a user's device. What I need is similar to gmail; except with gmail email addresses are used. For the contacts, I have the following needs:

  • each phone number must be an entry. So if John has 3 numbers (home, cell, work), they show as 3 entries

  • each entry is searchable by phone number or by first/last name of person

To create my adapter, I try to modify the one provided by Google but when I download the sample, it does not compile (kind of a crappy experience when the thing is right out of the box, but I am trying troubleshoot it). Then using the sample at http://developer.android.com/reference/android/widget/MultiAutoCompleteTextView.html I will bound my MultiAutoCompleteTextView to the adapter. At this point, I am not sure how to convert the adapter to match my needs (i.e. search contacts by name or phone and to retrieve the numbers). So my call for help is this: has anyone successfully done this and don't mind sharing their code? Or Does anyone know how I can modify the linked adapter to give me phone numbers, which I can search by name or phone? And third, will the adapter work with MultiAutoCompleteTextView?

Note

In asking this question, I have made certain assumptions on how Google is implementing their MultiAutoCompleteTextView for emails. Does anyone know if that code is open source? Does anyone know if my assumptions are correct? Will my idea for implementing my contact phone MultiAutoCompleteTextView work?

UPDATE

So I have come a long way since asking the question. I am now using the answer at AutoComplete with name and number as in native sms app Android . But I am trying to convert the implementation to MultiAutoCompleteTextView but it's not allowing for multiple entries. Does anyone know how I might finish this?

UPDATE 2

Refer to AutoComplete with name and number as in native sms app Android :

My MultiAutoCompleteTextView is presently kind of working: it's allowing for multiple entries. I simply replaced AutoCompleteTextView with MultiAutoCompleteTextView, and I ignored the other answer's onItemClick suggestion. It's working, kind of. Except, the data I get is not the nice formatted elements that you see in the gmail EditText. So back to the original question: how is Google doing it? I don't want to spend time explaining how the gmail compose editText looks as the relevant reader can readily verify this. In their EditText I can enter four contacts and then with random access click on one to delete it. I want to be able to do that. How?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this:

final Resources res = getResources();
LinearLayout ll = new LinearLayout(this);
AutoCompleteTextView tv = new AutoCompleteTextView(this);
tv.setThreshold(1);
String[] from = { Phone.DISPLAY_NAME };
int[] to = { android.R.id.text1 };
SimpleCursorAdapter a = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
a.setStringConversionColumn(2); // Phone.NUMBER
ViewBinder viewBinder = new ViewBinder() {
    @Override
    public boolean setViewValue(View v, Cursor c, int index) {
        TextView tv = (TextView) v;
        int typeInt = c.getInt(3); // Phone.TYPE
        CharSequence type = Phone.getTypeLabel(res, typeInt, null);
        // Phone.DISPLAY_NAME + Phone.NUMBER + type
        tv.setSingleLine(false);
        tv.setText(c.getString(1) + "
" + c.getString(2) + " " + type);
        return true;
    }
};
a.setViewBinder(viewBinder);
FilterQueryProvider provider = new FilterQueryProvider() {
    @Override
    public Cursor runQuery(CharSequence constraint) {
        // run in the background thread
        Log.d(TAG, "runQuery constraint: " + constraint);
        if (constraint == null) {
            return null;
        }
        ContentResolver cr = getContentResolver();
        Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, constraint.toString());
        String[] proj = { BaseColumns._ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone.TYPE, };
        return cr.query(uri, proj, null, null, null);
    }
};
a.setFilterQueryProvider(provider);
tv.setAdapter(a);
ll.addView(tv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setContentView(ll);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...