Last active
December 19, 2015 04:28
-
-
Save jmhend/5896838 to your computer and use it in GitHub Desktop.
Prevent notifying each of the ArrayAdapter's DataSetObservers on every single add(T object) call to the ArrayAdapter's dataset. Use similar logic for remove() calls.
This file contains hidden or 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 BetterArrayAdapter<T> extends ArrayAdapter<T> { | |
//... | |
@Override | |
public void addAll(Collection<? extends T> collection) { | |
// Firmware is at least API 11, when ArrayAdapter#addAll() was introduced. | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { | |
super.addAll(collection); | |
} else { | |
// Disable DataSetObserver notifications. | |
setNotifyOnChange(false); | |
// Add each datum to the dataset. | |
for (T object : collection) { | |
add(object); | |
} | |
// Notify each DataSetObserver. | |
// Note: Calling this method also internally calls 'setNotifyOnChange(true)' | |
notifyDataSetChanged(); | |
} | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment