Skip to content

Instantly share code, notes, and snippets.

@jmhend
Last active December 19, 2015 04:28
Show Gist options
  • Save jmhend/5896838 to your computer and use it in GitHub Desktop.
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.
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