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());
}
}
尽管使用ASC,联系人仍未排序?你可以帮助我解决它的原因吗? 我也使用了upper()方法
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null,null,&#34; upper(&#34; + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +&#34;)ASC&#34;);
答案 0 :(得分:0)
您将(已排序)结果放在HashMap中,而HashMap不保留元素的顺序。使用列表,它将起作用。
修改:使用您在评论中提出的Tuple
课程,它应如下所示:
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);
}
}
但我建议在这种情况下将Tuple
类重命名为Contact
。有一个名为&#39; name&#39;的成员的元组课程。和&#39;数字&#39;很奇怪。