public void ReadContacts() {
Cursor people = getContentResolver().query(Phone.CONTENT_URI, null, null, null,Phone.DISPLAY_NAME + " ASC ");
int indexName = people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
name = people.getString(indexName);
number = people.getString(indexNumber);
contacts.put(name, number);
} while (people.moveToNext());
printHashMap(contacts);
}
public void printHashMap(HashMap<String, String> a) {
for (Entry<String, String> lists : a.entrySet()) {
Log.d(lists.getKey(), lists.getValue());
}
}
The contacts are not sorted inspite of using ASC ? can you help me out with the reason for it ? I used upper() method also also
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
You put your (sorted) results in a HashMap, which does not preserve the order of elements. Use a List and it will work.
Edit: Using the Tuple
class you propose in your comment, it should look like this:
static class Tuple {
public String name;
public String number;
public Tuple(String name, String number) {
this.name = name;
this.number = number;
}
}
public void ReadContacts() {
Cursor people = getContentResolver().query(Phone.CONTENT_URI, null, null, null, Phone.DISPLAY_NAME + " ASC ");
int indexName = people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
List<Tuple> contacts = new ArrayList<Tuple>();
people.moveToFirst();
do {
name = people.getString(indexName);
number = people.getString(indexNumber);
contacts.add(new Tuple(name, number));
} while (people.moveToNext());
printHashMap(contacts);
}
public void printList(List<Tuple> list) {
for (Tuple tuple : list) {
Log.d(tuple.name + ", " + tuple.number);
}
}
But I recommend renaming the Tuple
class to Contact
in this case. Having a tuple class with members named 'name' and 'number' is odd.