1

I am building an application, I want to be able to type in a contact's name through an AutoCompleteTextView, then after selecting the contact, I want to be able to add that contact to a ListView. So far I have been able to receive the contact name, phone number, and type through the autocomplete with this code:

public class UserContactActivity 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.activity_user_contacts);

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

        // This is the button that updates the user's list of emergency contacts
        //Button btnSimple = (button) findViewById(R.id.btnSimple);

        //btnSimple.setOnClickListener(new OnClickListener() {
            //public void onClick(View v) (
                // adds contact
                //.notifyDataSetChanged();

    }

    //}

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

Now I want to be able to add the selected contact to a listview and just display their name and number. This is my current java file with the arrayadapter and list:

public class MainActivity extends Activity {

    final ArrayList<String> phoneList = new ArrayList<String>();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ListView myListView = (ListView)findViewById(R.id.myListView);
            final EditText myEditText = (EditText)findViewById(R.id.myEditText);
            final ArrayList<String> phoneList = new ArrayList<String>();
            final ArrayAdapter<String> aa;

            aa=new ArrayAdapter<String>(this, R.layout.custom_list_item, R.id.phoneName, phoneList);
            myListView.setAdapter(aa);

            Button btnSimple = (Button) findViewById(R.id.btnSimple);
            btnSimple.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    //adds contact
                    phoneList.add(0, myEditText.getText().toString());
                    //update the view
                    aa.notifyDataSetChanged();
                    //erases text to add phone
                    myEditText.setText("");
                }
            });
        }
    }

Sorry if this is a simple question. I am new to programming for android, but have been trying to get this to work for days. Any help would be very much appreciated thanks for looking.

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
user3566896
  • 97
  • 3
  • 9

1 Answers1

0

instead of using ArrayAdapter you can use BaseAdapter And Pass your custom Listview cell layout .or extends ArrayAdapter to your own cudtom class. and inflate your layout.

for more info

http://developer.android.com/reference/android/widget/ArrayAdapter.html

http://www.vogella.com/tutorials/AndroidListView/article.html

mcd
  • 1,434
  • 15
  • 31
  • Thank you for the response, I will look at the links you posted and see if I can come up with the solution. – user3566896 Apr 24 '14 at 03:12
  • I looked over the two links, but unfortunately I am still lost. The examples have string values already created. I am looking for a way to have the name and phone number of the contact selected in the autocomplete textview to appear as string values. Do I need to first store the name and number to a database? – user3566896 Apr 24 '14 at 05:30
  • so from your code you store all your phone contacts in hashmap and hashmap are store in arraylist.you can serach your contact in listview from the edittext serach by user and display in listview – mcd Apr 24 '14 at 06:37
  • the contacts are in hashmap, but I guess I'm not sure how to store or call the hashmap in the listview. The code I am using for the autocomplete is very similar, if not the same as the code presented in this question: http://stackoverflow.com/questions/12400504/selecting-contact-from-autocomplete-textview?rq=1. However, I don't have the reputation required to post images. – user3566896 Apr 24 '14 at 06:41