我有2个列表,一个是名字,另一个是数字。当我对名称进行排序时,其工作正常,但数字与这些名称不匹配,这是我的代码

try {
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };
        List<String> contactNames = new ArrayList<String>();
        List<String> contactNumbers = new ArrayList<String>();
        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);
            contactNames.add(name);
            contactNumbers.add(number);
        } while (people.moveToNext());
        PhoneContactsAdapter adapter = new PhoneContactsAdapter(context,
                contactNames, contactNumbers, selectedContacts);
        Collections.sort(contactNames);
        adapter.notifyDataSetChanged();
        lv_contacts_phone.setAdapter(adapter);
        lv_contacts_phone.setFastScrollEnabled(true);
    } catch (Exception e) {
        System.out
                .println("(SELECTFRIENDSACTIVITY)Selecting contacts from friends: "
                        + e);
    }


这里,contactNames和contactNumbers被赋予列表视图lv_contacts_phone在这里,我仅获得排序的姓名,而电话号码与姓名的匹配不正确,请帮助我

最佳答案

而不是将人的contactNumbercontactName存储在单独的List中。您可以创建一个封装了PersoncontactName的类contactNumber。填充List<Person>然后对其进行排序。或使用可以使用Map<String,String>,其中键将为contactNumber并指定contactName

08-27 15:40