【问题标题】:Android MultiAutoCompleteTextView to retrieve multiple contactsAndroid MultiAutoCompleteTextView 检索多个联系人
【发布时间】:2013-05-26 17:39:26
【问题描述】:

我正在尝试修改我在 stackoverflow 上看到的 AutoCompleteTextView 的代码,以便为 MultiAutoCompleteTextView 返回多个联系人。

但是,当我将它加载到我的 Android 手机上时,我再也看不到任何可供选择的建议。我感觉问题在于 ArrayAdapter 初始化,但我无法弄清楚它有什么问题。

提前感谢您的帮助。

我的 multiautocompletetextview 有以下代码:

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

最后我有以下内容可以阅读我从selecting contact from autocomplete textview 获取的联系人,并稍作修改。

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);
}

            });

【问题讨论】:

    标签: android android-arrayadapter autocompletetextview contactscontract


    【解决方案1】:

    我所做的是,我没有使用 ArrayList&lt;Map&lt;String, String&gt;&gt; 作为我的 ArrayAdapter 类型,而是创建了一个名为 ContactsInfo 的类,然后创建了一个 ArrayAdapter&lt;ContactsInfo&gt;

    同样,我将我的 ArrayList&lt;Map&lt;String, String&gt;&gt; 更改为 ArrayList&lt;ContactsInfo&gt; 并且它起作用了。

    您还必须覆盖 ContactsInfo 类中的 toString 方法。

    【讨论】:

    • 我遵循了您提供的相同示例,并且工作正常。但问题是,它不显示逗号。我必须手动添加它。
    【解决方案2】:

    您遇到的问题是因为您使用的是 ArrayAdaptor。您正在给它一个地图列表以选择表单,但没有关于如何过滤或比较地图对象与输入文本的数据。我改用了 SimpleAdapter,效果很好。

    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);
    

    另外,在你的 OnClickListener 中一定要连接联系人的名字。 改变:

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

    收件人:

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

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 2012-08-26
      相关资源
      最近更新 更多