Autocomplete with name and number, as in native sms application for Android

I want to add an AutoCompleteTextView to my application and find contacts by name and number, as is done in the native SMS application with Android. I looked on the Internet and tried a lot of things, but I want my application to display it as an Android application for Android. Here is the code I'm trying to find, only Display_Name .

 public class MakePayment extends Activity { private AutoCompleteTextView mAuto; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.makepayment); mAuto = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewTest); ContentResolver content = getContentResolver(); Cursor cursor = content.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PEOPLE_PROJECTION, null, null, null); ContactListAdapter adapter = new ContactListAdapter(this, cursor); mAuto.setAdapter(adapter); } public static class ContactListAdapter extends CursorAdapter implements Filterable { public ContactListAdapter(Context context, Cursor c) { super(context, c); mContent = context.getContentResolver(); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { final LayoutInflater inflater = LayoutInflater.from(context); final TextView view = (TextView) inflater.inflate( android.R.layout.simple_expandable_list_item_1, parent, false); view.setText(cursor.getString(2)); return view; } @Override public void bindView(View view, Context context, Cursor cursor) { ((TextView) view).setText(cursor.getString(2)); } @Override public String convertToString(Cursor cursor) { return cursor.getString(2); } @Override public Cursor runQueryOnBackgroundThread(CharSequence constraint) { if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); } StringBuilder buffer = null; String[] args = null; if (constraint != null) { buffer = new StringBuilder(); buffer.append("UPPER("); buffer.append(ContactsContract.Contacts.DISPLAY_NAME); buffer.append(") GLOB ?"); args = new String[] { constraint.toString().toUpperCase() + "*" }; } return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PEOPLE_PROJECTION, buffer == null ? null : buffer.toString(), args, null); } private ContentResolver mContent; } private static final String[] PEOPLE_PROJECTION = new String[] { ContactsContract.Contacts._ID, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.Contacts.DISPLAY_NAME, }; } 

How do I search by phone number? And then display it like this: enter image description here

+4
source share
2 answers

I already did something similar in the past, which I did:

Note: you will get autocomplete after writing two characters

AutoCompleteView is used in the layout file, basically it is an EditText with a drop-down list that appears when you enter it. The XML file in your example is as follows:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <AutoCompleteTextView android:id="@+id/mmWhoNo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="To...." > </AutoCompleteTextView> </LinearLayout> 

To create the custom view used in AutoCompleteView, you must declare another custxview.xml .xml file and it will look like this:

 <TextView android:id="@+id/ccontName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textColor="#A5000000" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/ccontNo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/ccontName" android:text="Medium Text" android:textColor="#A5000000" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/ccontType" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/ccontNo" android:layout_alignParentRight="true" android:layout_marginRight="14dp" android:text="Small Text" android:textColor="#A5000000" android:textAppearance="?android:attr/textAppearanceSmall" /> 

now In your activity:

 public class ContactActivity extends Activity { private ArrayList<Map<String, String>> 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<Map<String, String>>(); 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<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); } } 

Do not forget to add

 <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> 

in your manifest

+11
source

AutoCompleteTextView was completed using the @K_Anas code. Since it returns a pair (key, value), I used the following to display only the number of the selected item:

 @Override public void onItemClick(AdapterView<?> av, View v, int index, long arg) { Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index); Iterator<String> myVeryOwnIterator = map.keySet().iterator(); while(myVeryOwnIterator.hasNext()) { String key=(String)myVeryOwnIterator.next(); String value=(String)map.get(key); mTxtPhoneNo.setText(value); } } }); 

Things achieved in this matter:

  • AutoCompleteTextView for loading contacts in the same format as in the image in the question above.
  • Set the text as the phone number of the selected value.
0
source

All Articles