MultiAutoCompleteTextView与联系人电话号码 [英] MultiAutoCompleteTextView with contacts phone numbers

本文介绍了MultiAutoCompleteTextView与联系人电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建与用户的设备上的联系人的电话号码一MultiAutoCompleteTextView。我需要的是类似于Gmail的;除了与Gmail电子邮件地址被使用。对于接触,我有以下需求:

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:

  • 每个电话号码必须是一个条目。因此,如果约翰有3个号码(家庭,小区,工作),他们表现出的3项

  • 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

要创建我的适配器,我尝试修改由谷歌提供一个但是当我下载示例,它不编译(那种一个蹩脚的经验,当事情是​​正确的开箱即用,但我试图解决它)。然后用样品在的http://developer.android.com/reference/android/widget/MultiAutoCompleteTextView.html我会束缚我MultiAutoCompleteTextView到适配器。在这一点上,我不知道如何转换适配器符合我的需求(通过名称或电话即搜索联系人和检索数字)。所以,我的呼救是这样的:有没有人成功地做到了这一点,并且不介意分享自己的code?或者有谁知道我可以修改链接的适配器给我电话号码,我可以按名称或电话搜索?第三,将与MultiAutoCompleteTextView适配器的工作?

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?

注意

在问这个问题,我已经对谷歌是如何实施他们MultiAutoCompleteTextView的电子邮件某些假设。有谁知道,如果code是开源?有谁知道,如果我的假设是正确的?将我的想法实现我的联系电话MultiAutoCompleteTextView工作?

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?

更新

所以,我已经走过了漫长的道路,因为问的问题。我现在用的答案在<一个href="http://stackoverflow.com/questions/11147311/autocomplete-with-name-and-number-as-in-native-sms-app-android?rq=1">AutoComplete与名称和编号为在原生短信应用程序的Andr​​oid 。但我想实施MultiAutoCompleteTextView转换,但它不是允许多个条目。有谁知道我可能会完成这个?

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?

更新2

请参照<一个href="http://stackoverflow.com/questions/11147311/autocomplete-with-name-and-number-as-in-native-sms-app-android?rq=1">AutoComplete与名称和编号为在原生短信应用程序的Andr​​oid :

我的MultiAutoCompleteTextView是$ P $种psently工作:它允许多个条目。我只是代替 AutoCompleteTextView MultiAutoCompleteTextView ,我忽略了对方的回答的 onItemClick 的建议。它的工作样的。除了,我得到的数据是不是您在Gmail中看到漂亮的格式化元素的EditText 。因此,回到原来的问题:如何在谷歌做什么呢?我不想花时间解释如何Gmail撰写EDITTEXT看来,相关读者可以很容易地验证这一点。在他们的的EditText 我可以进入在一个四个触点,然后用随机访问点击将其删除。我希望能够做到这一点。怎么样?

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?

推荐答案

试试这个:

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) + "\n" + 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);

这篇关于MultiAutoCompleteTextView与联系人电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆