Select 多个联系电话列表中的一个联系电话

Select one contact number from a list of multiple contact numbers

cursor 的帮助下,我只想从特定用户的联系电话列表中获取第一个联系电话。这是我的代码:

private ArrayList<ArrayList<String>> getAllContacts() {

        ArrayList<ArrayList<String>> nameList = new ArrayList<ArrayList<String>>();
        ArrayList<String> person=new ArrayList<>();
        ArrayList<String> number=new ArrayList<>();
        ArrayList<String> temp=new ArrayList<>();
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

        if ((cur!=null ? cur.getCount() : 0) > 0) {
            while (cur!=null && cur.moveToNext()) {

                String id = cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(
                        ContactsContract.Contacts.DISPLAY_NAME));
                person.add(name);

                if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                    Cursor pCur = cr.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{id}, null);

                    if(pCur.getCount()==1) {
                        while (pCur.moveToNext()) {
                            String phoneNo = pCur.getString(pCur.getColumnIndex(
                                    ContactsContract.CommonDataKinds.Phone.NUMBER));
                            number.add(phoneNo);
                        }
                    }
                    else{
                        while (pCur.moveToNext()) {
                            String phoneNo = pCur.getString(pCur.getColumnIndex(
                                    ContactsContract.CommonDataKinds.Phone.NUMBER));
                            temp.add(phoneNo);
                        }
                        number.add(temp.get(0));
                        temp.clear();
                    }
                    pCur.close();
                }
            }
        }
        if (cur!=null) {
            cur.close();
        }
        Log.d("contacts",String.valueOf(number.size())+" "+String.valueOf(person.size())); //the lists aren't of equal size

        if(person.size()==number.size()){
            nameList.add(person);
            nameList.add(number);
        }
        else{
            //don't know what to do here
        }
        return nameList;

    }

但是,代码仍然会获取为单个用户保存的多个联系电话,换句话说 person.size() 不等于 number.size()。我该怎么办?

数组大小不一样,因为并非所有联系人都有 phone 号码,您正在正确检查 HAS_PHONE_NUMBER,并且只有在为真时,才会获取该联系人的 phone 号码 -这意味着 number.size() 在大多数 phone 上将是 < person.size()

我建议不要为姓名和 phone 保留单独的数组,而是使用一个包含代表一个人的简单 java class 的数组。

除此之外,您的代码非常低效,因为您正在执行大量查询,而您只能执行一个查询。

下面是包含上述两个建议的示例:

class Person {
    long id,
    String name,
    String firstPhone;

    public Person(id, name, firstPhone) {
        this.id = id;
        this.name = name;
        this.firstPhone = firstPhone;
    }
}

Map<Long, Person> mapping = new HashMap<>(); // mapping between a contact-id to a Person object

String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Phone.NUMBER};

// query phones only
String selection = Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);

while (cur != null && cur.moveToNext()) {
    long id = cur.getLong(0);
    String name = cur.getString(1); // full name
    String phone = cur.getString(2); // phone

    Log.d(TAG, "got " + id + ", " + name + " - " + data);

    // only add a new object if we haven't seen this person before
    if (!mapping.containsKey(id)) {
        Person person = new Person(id, name, phone);
        mapping.put(id, person);
    }
}
cur.close();

Array<Person> people = mapping.values();

编辑

Set<Long> ids = new HashSet<Long>();
Array<String> names = new ArrayList<String>();
Array<String> numbers = new ArrayList<String>();

String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Phone.NUMBER};

// query phones only
String selection = Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);

while (cur != null && cur.moveToNext()) {
    long id = cur.getLong(0);
    String name = cur.getString(1); // full name
    String phone = cur.getString(2); // phone

    Log.d(TAG, "got " + id + ", " + name + " - " + data);

    // only add a new object if we haven't seen this person before
    if (ids.add(id)) {
        names.add(name);
        numbers.add(phone);
    }
}
cur.close();