sarabu
sarabu

Reputation: 521

how to sort 2 lists at once in android

I have 2 lists one is names and other is numbers. When i sort names list its working fine but numbers are not matching with those names and this is my code

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);
    }

Here contactNames and contactNumbers are givent to the listview lv_contacts_phone here I get only sorted names and numbers were not matched properly with the names please help me

Upvotes: 2

Views: 238

Answers (3)

sarabu
sarabu

Reputation: 521

I got a great solution

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);
    }

Upvotes: 0

Hiren Dabhi
Hiren Dabhi

Reputation: 3713

user this code to sort contact by name.

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);
    }

here is comparator class:

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

data(Person) class:

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;
}

}

now contacts has sorted data, you can set in adapter.

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49432

Rather than storing the contactNumber and contactName of people in separate Lists. You can create a class Person which encapsulates contactName and contactNumber . Populate the List<Person> and then sort it . Or use can use a Map<String,String> where key will be contactNumber and value the contactName.

Upvotes: 1

Related Questions