Skip to content

Instantly share code, notes, and snippets.

@rivancic
Last active December 21, 2015 13:06
Show Gist options
  • Save rivancic/db1db4369a4d21a9ca35 to your computer and use it in GitHub Desktop.
Save rivancic/db1db4369a4d21a9ca35 to your computer and use it in GitHub Desktop.
Sorting Android Adapter data
public class CustomAdapter extends ArrayAdapter<Model> {
/**
* Sort should be enabled so the items are always consistently displayed.
* First disable notifying of the list because sort method does it internally therefore we can end up in loop.
* After sorting is done, enable notifying.
*/
@Override
public void notifyDataSetChanged() {
this.setNotifyOnChange(false);
this.sort(Model.getComparator());
this.setNotifyOnChange(true);
super.notifyDataSetChanged();
}
}
public class Model {
/**
* Creates basic comparator for models.
* @return basic comparator.
*/
public static Comparator<Model> getComparator() {
return new Comparator<Model>() {
@Override
public int compare(Model model1, Model
model2) {
return // implement the comparation.
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment