Android的MultiAutoCompleteTextView检索多个联系人 [英] Android MultiAutoCompleteTextView to retrieve multiple contacts

查看:165
本文介绍了Android的MultiAutoCompleteTextView检索多个联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改code,我看到的计算器一个AutoCompleteTextView,这样我会回到复式联系人进行MultiAutoCompleteTextView。

然而,当我加载此到我的Andr​​oid手机我再也不能看到任何建议,以供选择。我已经觉得问题出在一个ArrayAdapter初始化,但我无法弄清楚什么是错的。

感谢您事先的任何帮助。

我有以下的code我multiautocompletetextview:

 < MultiAutoCompleteTextView
     机器人:ID =@ + ID / mmWhoNo
     机器人:layout_width =FILL_PARENT
     机器人:layout_height =WRAP_CONTENT
     机器人:文字颜色=#0000A0
     机器人:提示=选择你的联系人/>

最后,我有以下阅读我的联系人我从<一个拿href=\"http://stackoverflow.com/questions/12400504/selecting-contact-from-autocomplete-textview\">selecting从自动完成的TextView 接触修改一点点。

 私人的ArrayList&LT;地图&LT;字符串,字符串&GT;&GT; mPeopleList;
私人ArrayAdapter mAdapter;
私人MultiAutoCompleteTextView mTxtPhoneNo;      mPeopleList =新的ArrayList&LT;地图&LT;字符串,字符串&GT;&GT;();
      PopulatePeopleList();
     mTxtPhoneNo =(MultiAutoCompleteTextView)findViewById(R.id.mmWhoNo);
     mTxtPhoneNo.setTokenizer(新MultiAutoCompleteTextView.CommaTokenizer());
     mTxtPhoneNo.setThreshold(1);             //只是为了检查看到mPeopleList正在填充
     Log.i(Multiplecontacts,mPeopleList.get(0)的ToString());
     mAdapter =新ArrayAdapter&LT; ArrayList的&LT;地图&LT;字符串,字符串&GT;&GT;&GT;(这一点,android.R.layout.simple_dropdown_item_1line);
     mAdapter.add(mPeopleList);     mTxtPhoneNo.setAdapter(mAdapter);    mTxtPhoneNo
            .setOnItemClickListener(新OnItemClickListener(){                @覆盖
                公共无效onItemClick(适配器视图&LT;&GT; AV,查看ARG1,
                        INT指数,长ARG3){
                    // TODO自动生成方法存根
                    地图&LT;字符串,字符串&GT;地图=(地图&LT;字符串,字符串&GT;)AV
                            .getItemAtPosition(索引);                    字符串名称= map.get(名称);
                    串号= map.get(手机);
                    mTxtPhoneNo.setText(+姓名+&LT;+数字+&gt;中);                }
公共无效PopulatePeopleList(){
    mPeopleList.clear();
    光标人= getContentResolver()查询(
            ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);
    而(people.moveToNext()){
        字符串CONTACTNAME = people.getString(人
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        字符串的ContactID = people.getString(人
                .getColumnIndex(ContactsContract.Contacts._ID));
        字符串hasPhone =人
                .getString(人
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));        如果((的Integer.parseInt(hasPhone)0)){
            //你知道有多少所以现在查询像这样
            光标手机= getContentResolver()查询(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    空值,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            +=+的ContactID,NULL,NULL);
            而(phones.moveToNext()){
                //存储号码并显示一个对话框让用户
                //选择哪个。
                字符串phoneNumber的=手机
                        .getString(电话
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                字符串numberType =手机
                        .getString(电话
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                地图&LT;字符串,字符串&GT; NamePhoneType =新的HashMap&LT;字符串,字符串&GT;();
                NamePhoneType.put(名,联系人姓名);
                NamePhoneType.put(手机,phoneNumber的);
                如果(numberType.equals(0))
                    NamePhoneType.put(类型,工作);
                否则如果(numberType.equals(1))
                    NamePhoneType.put(类型,家);
                否则如果(numberType.equals(2))
                    NamePhoneType.put(类型,手机);
                其他
                    NamePhoneType.put(类型,其他);
                //然后这个地图添加到列表中。
                mPeopleList.add(NamePhoneType);
            }
            phones.close();
        }
    }
    people.close();
    startManagingCursor(人);
}            });


解决方案

您所遇到的问题是因为您使用的是ArrayAdaptor。你给它的地图列表中选择的形式,但对如何过滤器或地图对象比较输入文本没有数据。我用了一个SimpleAdapter,而不是和它工作得很好。

  mTxtPhoneNo =(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextViewContactsNames);
mAdapter =新SimpleAdapter(这一点,mPeopleList,R.layout.costom_contact_view,新的String [] {姓名,手机,类型},新的INT [] {
                             R.id.ccontName,R.id.ccontNo,R.id.ccontType});
mTxtPhoneNo.setThreshold(1);
mTxtPhoneNo.setAdapter(mAdapter);
mTxtPhoneNo.setTokenizer(新MultiAutoCompleteTextView.CommaTokenizer());
mTxtPhoneNo.setOnItemClickListener(multiAutoContactNamesListener);

此外,你OnClickListener一定要串连联系人的姓名。
变化:

  mTxtPhoneNo.setText(+姓名+&LT;+数字+&gt;中);

要:

  mTxtPhoneNo.append(,+名字+&LT;+数字+&gt;中);

希望这有助于。

I am trying to modify a code that I have seen on stackoverflow for an AutoCompleteTextView so that i would return mutliple contacts for a MultiAutoCompleteTextView.

However, when I load this onto my Android phone I can no longer see any suggestions to select from. I have feeling that the problem lies in the ArrayAdapter initialization but I cannot figure out what is wrong with it.

Thanks in advance for any help.

I have the following code for my multiautocompletetextview:

  <MultiAutoCompleteTextView
     android:id="@+id/mmWhoNo"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:textColor="#0000A0"
     android:hint="Choose your Contacts" />

And finally I have the following to read my contacts which i have take from selecting contact from autocomplete textview with a little bit of modification.

private ArrayList<Map<String, String>> mPeopleList;
private ArrayAdapter mAdapter;
private MultiAutoCompleteTextView mTxtPhoneNo;

      mPeopleList = new ArrayList<Map<String, String>>();
      PopulatePeopleList(); 
     mTxtPhoneNo = (MultiAutoCompleteTextView) findViewById(R.id.mmWhoNo);
     mTxtPhoneNo.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); 
     mTxtPhoneNo.setThreshold(1);

             //just to check to see that mPeopleList is being populated
     Log.i("Multiplecontacts",mPeopleList.get(0).toString());


     mAdapter = new ArrayAdapter<ArrayList<Map<String,String>>>(this, android.R.layout.simple_dropdown_item_1line);
     mAdapter.add(mPeopleList);

     mTxtPhoneNo.setAdapter(mAdapter);

    mTxtPhoneNo
            .setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> av, View arg1,
                        int index, long arg3) {
                    // TODO Auto-generated method stub
                    Map<String, String> map = (Map<String, String>) av
                            .getItemAtPosition(index);

                    String name = map.get("Name");
                    String number = map.get("Phone");
                    mTxtPhoneNo.setText("" + name + "<" + number + ">,");

                }


public void PopulatePeopleList() {
    mPeopleList.clear();
    Cursor people = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while (people.moveToNext()) {
        String contactName = people.getString(people
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        String contactId = people.getString(people
                .getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = people
                .getString(people
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

        if ((Integer.parseInt(hasPhone) > 0)) {
            // You know have the number so now query it like this
            Cursor phones = getContentResolver().query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = " + contactId, null, null);
            while (phones.moveToNext()) {
                // store numbers and display a dialog letting the user
                // select which.
                String phoneNumber = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                String numberType = phones
                        .getString(phones
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                Map<String, String> NamePhoneType = new HashMap<String, String>();
                NamePhoneType.put("Name", contactName);
                NamePhoneType.put("Phone", phoneNumber);
                if (numberType.equals("0"))
                    NamePhoneType.put("Type", "Work");
                else if (numberType.equals("1"))
                    NamePhoneType.put("Type", "Home");
                else if (numberType.equals("2"))
                    NamePhoneType.put("Type", "Mobile");
                else
                    NamePhoneType.put("Type", "Other");
                // Then add this map to the list.
                mPeopleList.add(NamePhoneType);
            }
            phones.close();
        }
    }
    people.close();
    startManagingCursor(people);
}

            });

解决方案

The problem you are having is because you are using an ArrayAdaptor. You are giving it a list of Map to choose form but no data on how to filter or compare the map objects to the input text. I used a SimpleAdapter instead and it worked fine.

mTxtPhoneNo = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextViewContactsNames);
mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.costom_contact_view, new String[] { "Name", "Phone", "Type" }, new int[] {
                             R.id.ccontName, R.id.ccontNo, R.id.ccontType });
mTxtPhoneNo.setThreshold(1);
mTxtPhoneNo.setAdapter(mAdapter);
mTxtPhoneNo.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
mTxtPhoneNo.setOnItemClickListener(multiAutoContactNamesListener);

Also, in you OnClickListener be sure to concatenate the contacts' names. Change :

mTxtPhoneNo.setText("" + name + "<" + number + ">,");

To:

mTxtPhoneNo.append(", " + name + " <" + number + ">");

Hope this helps.

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

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