简体   繁体   中英

Android MultiAutoCompleteTextView to retrieve multiple contacts

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

            });

what i did was that instead of using ArrayList<Map<String, String>> for my ArrayAdapter type, i created a class called ContactsInfo and then created an ArrayAdapter<ContactsInfo> .

And similarly i changed my ArrayList<Map<String, String>> to ArrayList<ContactsInfo> and it worked.

Also you have to overwrite the toString method in your ContactsInfo class.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM