带有联系人电话号码的 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:

  • 每个电话号码都必须是一个条目.因此,如果 John 有 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

为了创建我的适配器,我尝试修改 Google 但是当我下载示例时,它无法编译(当这个东西开箱即用时,这是一种糟糕的体验,但我正在尝试对其进行故障排除).然后使用 http://developer.android.com/reference/android 上的示例/widget/MultiAutoCompleteTextView.html 我会将我的 MultiAutoCompleteTextView 绑定到适配器.在这一点上,我不确定如何转换适配器以满足我的需要(即按姓名或电话搜索联系人并检索号码).所以我的求助是这样的:有没有人成功地做到了这一点并且不介意分享他们的代码?或者有谁知道我如何修改链接的适配器来给我电话号码,我可以按姓名或电话搜索?第三,适配器是否可以与 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?

注意

在问这个问题时,我对 Google 如何为电子邮件实施 MultiAutoCompleteTextView 做了一些假设.有谁知道该代码是否开源?有谁知道我的假设是否正确?我实现联系电话 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?

更新

所以自从提出这个问题以来,我已经走了很长一段路.我现在使用 使用名称和号码自动完成 Android 原生短信应用.但我试图将实现转换为 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

参考自动完成使用 Android 原生短信应用程序中的名称和号码 :

我的 MultiAutoCompleteTextView 目前可以正常工作:它允许多个条目.我只是用 MultiAutoCompleteTextView 替换了 AutoCompleteTextView,而忽略了其他答案的 onItemClick 建议.它的工作,有点.除了,我得到的数据不是你在 gmail EditText 中看到的漂亮的格式化元素.那么回到最初的问题:谷歌是如何做到的?我不想花时间解释 gmail compose 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) + "
" + 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天全站免登陆