2

我有 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被赋予listview lv_contacts_phone 这里我只得到排序的名字和数字与名字不正确匹配请帮助我

4

3 回答 3

1

而不是将人的contactNumberand存储contactName在单独List的 s 中。您可以创建一个Person封装contactNamecontactNumber. 填充List<Person>,然后对其进行排序。或者使用可以使用Map<String,String>where 键contactNumber和值contactName

于 2013-06-19T05:31:35.677 回答
0

使用此代码按姓名对联系人进行排序。

try {
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER };
        List<Person> contacts = new ArrayList<Person>();
        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 {
            Person person = new Person();
            String name = people.getString(indexName);
            String number = people.getString(indexNumber);
            person.setName(name);
            person.setNumber(number);
            contacts.add(person);
            Collections.sort(contacts, new ContactsComparator());
        } while (people.moveToNext());

    } catch (Exception e) {
        System.out
                .println("(SELECTFRIENDSACTIVITY)Selecting contacts from friends: "
                        + e);
    }

这是比较器类:

class ContactsComparator implements Comparator<Person> {
    public int compare(Person p1, Person p2) {          
        return p1.getName().compareTo(p1.getName());
    }
}

数据(人)类:

public class Person {
private String name;
private String number;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getNumber() {
    return number;
}
public void setNumber(String number) {
    this.number = number;
}

}

现在联系人已经排序数据,您可以在适配器中设置。

于 2013-06-19T06:55:39.830 回答
0

我有一个很好的解决方案

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);
        Map<String, String> mapStrings = new HashMap<String, String>();
        people.moveToFirst();
        do {
            String name = people.getString(indexName);
            String number = people.getString(indexNumber);
            mapStrings.put(name, number);
        } while (people.moveToNext());
        Map<String, String> sortedMap = new TreeMap<String, String>(
                mapStrings);
        for (Entry<String, String> entry : sortedMap.entrySet()) {
            contactNames.add(entry.getKey());
            contactNumbers.add(entry.getValue());
        }

        lv_contacts_phone.setAdapter(new PhoneContactsAdapter(context,
                contactNames, contactNumbers, selectedContacts));
        lv_contacts_phone.setFastScrollEnabled(true);

    } catch (Exception e) {
        System.out
                .println("(SELECTFRIENDSACTIVITY)Selecting contacts from friends: "
                        + e);
    }
于 2013-06-19T13:30:09.780 回答