Android MultiAutoCompleteTextView 检索多个联系人

use*_*865 1 android autocompletetextview android-arrayadapter 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" />
Run Code Online (Sandbox Code Playgroud)

最后,我有以下内容可以阅读我的联系人,这些联系人是从自动完成文本视图中选择联系人并稍加修改后获得的。

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

            });
Run Code Online (Sandbox Code Playgroud)

use*_*865 5

我所做的不是ArrayList<Map<String, String>>用于我的 ArrayAdapter 类型,而是创建了一个名为 ContactsInfo 的类,然后创建了一个ArrayAdapter<ContactsInfo>.

同样,我将我更改ArrayList<Map<String, String>>ArrayList<ContactsInfo>并且它起作用了。

此外,您必须覆盖toStringContactsInfo 类中的方法。