AutoComplete with name and number as in native sms app Android

前端 未结 2 740
青春惊慌失措
青春惊慌失措 2020-12-16 07:51

I want to add an AutoCompleteTextView in my app and search contacts by name and number as done in the native SMS app with android. I have looked on the internet

2条回答
  •  囚心锁ツ
    2020-12-16 08:44

    I have already done something similar in the past that's what I've done:

    note: you will get Autocompletion after taping two character

    The layout file uses an AutoCompleteView, its basically an EditText with a dropdown list that appears as you type in it. The .xml file in youe example looks like this:

    
    
    
    
    
    
    
    
    
    

    To create the custom view used in the AutoCompleteView you have to declare another .xml file called custcontview.xml and it will looks like this:

    
    
    
    
    
    

    now In your Activity:

    public class ContactActivity extends Activity {
    
        private ArrayList> mPeopleList;
    
        private SimpleAdapter mAdapter;
        private AutoCompleteTextView mTxtPhoneNo;
    
    /** Called when the activity is first created. */
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            mPeopleList = new ArrayList>();
            PopulatePeopleList();
            mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
    
            mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview ,new String[] { "Name", "Phone" , "Type" }, new int[] { R.id.ccontName, R.id.ccontNo, R.id.ccontType });
    
            mTxtPhoneNo.setAdapter(mAdapter);
    
            }
    
        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 NamePhoneType = new HashMap();
    
        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);
        }
        }
    

    Don't forget to add

    
    

    in your manifest

提交回复
热议问题