1

我正在尝试修改我在 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" />

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

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

            });
4

2 回答 2

5

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

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

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

于 2013-05-29T03:12:09.310 回答
0

您遇到的问题是因为您使用的是 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 + ">");

希望这可以帮助。

于 2013-05-28T21:59:40.420 回答