Android MultiAutoCompleteTextView可检索多个联系人

时间:2013-05-26 17:39:26

标签: android android-arrayadapter autocompletetextview contactscontract

我正在尝试修改我在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);
}

            });

2 个答案:

答案 0 :(得分:3)

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

同样我将我的ArrayList<Map<String, String>>更改为ArrayList<ContactsInfo>,但它确实有效。

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

答案 1 :(得分:0)

您遇到的问题是因为您使用的是ArrayAdaptor。您正在为其提供一个Map列表以选择表单,但没有关于如何过滤或比较地图对象与输入文本的数据。我使用了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 + ">");

希望这有帮助。