2

私は 2 つのリストを持っています。1 つは名前で、もう 1 つは数字です。名前のリストを並べ替えると正常に機能しますが、数字がそれらの名前と一致せず、これが私のコードです

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 に与えられます。ここでは、並べ替えられた名前のみが取得され、番号が名前と適切に一致しませんでした。助けてください

4

3 に答える 3

1

contactNumber人のとcontactNameを別々の に格納するのではなくList。とPersonをカプセル化したクラスを作成できます。にデータを入力してから並べ替えます。または、使用する場所キーを使用して、値を使用できます。contactNamecontactNumberList<Person>Map<String,String>contactNumbercontactName

于 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 に答える