Steve C. Steve C. - 2 years ago 194
Java Question

How to sort listview with remote data instantly using Comparator in Android?

When I click a button to sort the listview, it works but only if I start scrolling the listview does it show the sort order.

This is what I have in my data model class:

public static Comparator<Emplyoyee> sortNameAtoZ = new Comparator<Emplyoyee>() {

public int compare(Emplyoyee lhs, Emplyoyee rhs) {
String emp1 = lhs.getName();
String emp2 = rhs.getName();

return emp1.compareTo(emp2);
//descending order
//return StudentName2.compareTo(StudentName1);

When the user clicks the button, I call this:

Collections.sort(rowdata, EmployeeRowData.sortNameZtoA);

After doing the research and comparing different sources, the setup is the same as it is here but it is not sorting the listview instantly upon clicking the button. I'm populating the listview with data from a remote database using Volley.

Answer Source

try this:

Collections.sort(rowdata, EmployeeRowData.sortNameZtoA);



Your adaptor

public void setArrayList(ArrayList<Emplyoyee> array){
yourArray =array;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download