filipebatista
6/30/2013 - 8:56 PM

Prevent notifying each of the ArrayAdapter's DataSetObservers on every single add(T object) call to the ArrayAdapter's dataset. Use similar

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();
        }
    }

    // ...
}