0

我正在获取所有联系人姓名和号码 bur 我只想显示我选择的号码和姓名。

这是返回所有联系人的代码:

ArrayList<String> contactList = new ArrayList<String>();

    switch (reqCode) {
    case (0):
        if (resultCode == Activity.RESULT_OK) {

            Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] projection = new String[] {
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER };

            Cursor people = getContentResolver().query(uri, projection,
                    null, null, null);

            int indexName = people
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int indexNumber = people
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            people.moveToFirst();
            do {
                String name = people.getString(indexName);
                String number = people.getString(indexNumber);

                String contact = name + "" + number;
                contactList.add(contact);
            } while (people.moveToNext());

        }

contactList我想添加一个数字和一个名字

4

1 回答 1

1

据我了解您的问题,您希望在集合中添加数字和名称 - 为此使用 HashMap。

 HashMap<String,String> contactList = new HashMap<String,String>();

switch (reqCode) {
case (0):
    if (resultCode == Activity.RESULT_OK) {

        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };

        Cursor people = getContentResolver().query(uri, projection,
                null, null, null);

        int indexName = people
                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        int indexNumber = people
                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

        people.moveToFirst();
        do {
            String name = people.getString(indexName);
            String number = people.getString(indexNumber);

            String contact = name + "" + number;
            contactList.put(number,name);
        } while (people.moveToNext());

    }
于 2013-08-13T05:15:40.233 回答