按降序和升序对 phone 个联系人进行排序
sort phone contacts in descending and ascending order
我正在列表视图中加载我 phone 的联系人,并按升序对它们进行排序。
现在我有 2 个按钮:升序 和 降序。如何更改列表视图以根据单击的按钮按升序和降序显示联系人?
试试这个提升,
ArrayList<Contact> contactArrayList=new ArrayList<>();
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
//get the data of each contact and store in list (here the item is Contact object)
//for example
Contact contact=new Contact();
contact.setName("//get name of contact");
contact.setPhonenumber("//get phonenum of contact");
contactArrayList.add(contact);
}
phones.close();
Collections.sort(contactArrayList, new Comparator<Contact>() {
@Override
public int compare(Contact lhs, Contact rhs) {
//get the item which is to be compared
//here I have used name(String) of the contacts
String lhsString = lhs.getName();
String rhsString = rhs.getName();
return lhsString.compareToIgnoreCase(rhsString);
}
});
降序排列,
return rhsString.compareToIgnoreCase(lhsString);
我正在列表视图中加载我 phone 的联系人,并按升序对它们进行排序。
现在我有 2 个按钮:升序 和 降序。如何更改列表视图以根据单击的按钮按升序和降序显示联系人?
试试这个提升,
ArrayList<Contact> contactArrayList=new ArrayList<>();
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
//get the data of each contact and store in list (here the item is Contact object)
//for example
Contact contact=new Contact();
contact.setName("//get name of contact");
contact.setPhonenumber("//get phonenum of contact");
contactArrayList.add(contact);
}
phones.close();
Collections.sort(contactArrayList, new Comparator<Contact>() {
@Override
public int compare(Contact lhs, Contact rhs) {
//get the item which is to be compared
//here I have used name(String) of the contacts
String lhsString = lhs.getName();
String rhsString = rhs.getName();
return lhsString.compareToIgnoreCase(rhsString);
}
});
降序排列,
return rhsString.compareToIgnoreCase(lhsString);