Last active
December 21, 2015 13:06
-
-
Save rivancic/db1db4369a4d21a9ca35 to your computer and use it in GitHub Desktop.
Sorting Android Adapter data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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