简体   繁体   English

自动完成联系人并添加到ListView

[英]Autocomplete Contacts and add to ListView

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 . 我正在构建一个应用程序,我希望能够通过AutoCompleteTextView输入联系人的姓名,然后在选择联系人之后,我希望能够将该联系人添加到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 : 这是我当前的java文件,带有arrayadapterlist

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. 我是android编程的新手,但是已经尝试了好几天了。 Any help would be very much appreciated thanks for looking. 任何帮助将非常感谢您的光临。

instead of using ArrayAdapter you can use BaseAdapter And Pass your custom Listview cell layout .or extends ArrayAdapter to your own cudtom class. 除了使用ArrayAdapter之外,还可以使用BaseAdapter并传递自定义Listview单元布局。或将ArrayAdapter扩展到您自己的cudtom类。 and inflate your layout. 并扩大您的布局。

for more info 了解更多信息

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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