Using autocomplete textview with phone number

I’m autocompleting the edittext field for contact phone numbers. I know how to get contacts from the database and display them in a text box, but I just need to autocomplete them if the user wants to enter a name in the text box. I understand how to get an array for automatic completion and the whole theory. But how to pull phone contacts is difficult. I saw a lot of tutorials, as well as a different question about stack overflow, but still awkward. The code snippet will help you.

public class MyContacts extends Activity { AutoCompleteTextView txtPhoneNo; public ArrayList<String> c_Name = new ArrayList<String>(); public ArrayList<String> c_Number = new ArrayList<String>(); String[] name_Val = null; String[] phone_Val = null; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); txtPhoneNo = (AutoCompleteTextView) findViewById(R.id.txtPhoneNo); } Uri contacts = Uri.parse("content://icc/adn"); ContentResolver cr = getContentResolver(); Cursor managedCursor1 = cr.query(contacts, null, null, null, null); { if (managedCursor1.moveToFirst()) { String contactname; String cphoneNumber; int nameColumn = managedCursor1.getColumnIndex("name"); int phoneColumn = managedCursor1.getColumnIndex("number"); Log.d("int Name", Integer.toString(nameColumn)); Log.d("int Number", Integer.toString(phoneColumn)); do { // Get the field values contactname = managedCursor1.getString(nameColumn); cphoneNumber = managedCursor1.getString(phoneColumn); if ((contactname != " " || contactname != null) && (cphoneNumber != " " || cphoneNumber != null)) { c_Name.add(contactname); c_Number.add(cphoneNumber); } } while (managedCursor1.moveToNext()); } name_Val = (String[]) c_Name.toArray(new String[c_Name.size()]); phone_Val = (String[]) c_Number.toArray(new String[c_Name.size()]); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, name_Val); txtPhoneNo.setAdapter(adapter); } } 

My code ... no compilation errors, but still not working

+4
source share
3 answers

It would be better to get Contacts and store them in ArrayList . Then you can just bind the ArrayList with an AutoComplete TextView. You can then simply filter the rest of the part with an ArrayList when the user enters AutoComplete TextView text.

UPDATE

You can create a POJO class with the getter-installer contact_name and contact_number . Then create a List<POJO> list = new ArrayList<POJO>(); . Then just add contact_name and contact_number to list using the POJO class

 POJO pojo_obj = new POJO(); pojo_obj.setcontact_name(contactname); pojo_obj.setcontact_number(cphoneNumber); list.add(pojo_obj); 

Finally, set list to Adapter .

And then in the adapter class you can set it in TextView with

 list.get(position).getcontact_name(); list.get(position).getcontact_number(); 
+3
source

It can specify phone numbers or contact names as an adapter to automatically end text viewing.

In the link below you will find a good guide to achieve this requirement.

Get phone contacts and show it in AutoComplete TextView in Android

+2
source

I think you are testing in the SDK. The SDK does not have a SIM card and therefore will not work. Insert .APK into your phone and try again. It should work like a charm.

0
source

Source: https://habr.com/ru/post/1393297/


All Articles