Android Open Source - mobilib Mbl Base Adapter






From Project

Back to project page mobilib.

License

The source code is released under:

MIT License

If you think the Android project mobilib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.datdo.mobilib.base;
//  w w w .j  a va  2 s. c  o m
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.datdo.mobilib.util.MblUtils;

import junit.framework.Assert;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;
import android.widget.ListView;

/**
 * <pre>
 * An extension of {@link BaseAdapter}.
 * Make all data changes and calls of {@link #notifyDataSetChanged()} synchronized on main thread to prevent concurrency problems.
 * </pre>
 * @param <T> class of object bound with an item of {@link ListView}
 */
public abstract class MblBaseAdapter<T> extends BaseAdapter {

    @SuppressWarnings("serial")
    private final List<T> mData = new ArrayList<T>() {
        @Override
        public void clear() {
            Assert.assertTrue(MblUtils.isMainThread());
            super.clear();
        }

        @Override
        public void add(int index, T object) {
            Assert.assertTrue(MblUtils.isMainThread());
            super.add(index, object);
        }

        @Override
        public boolean add(T object) {
            Assert.assertTrue(MblUtils.isMainThread());
            return super.add(object);
        }

        @Override
        public boolean addAll(Collection<? extends T> collection) {
            Assert.assertTrue(MblUtils.isMainThread());
            return super.addAll(collection);
        }

        @Override
        public boolean addAll(int index, Collection<? extends T> collection) {
            Assert.assertTrue(MblUtils.isMainThread());
            return super.addAll(index, collection);
        }

        @Override
        public T remove(int index) {
            Assert.assertTrue(MblUtils.isMainThread());
            return super.remove(index);
        }

        @Override
        public boolean remove(Object object) {
            Assert.assertTrue(MblUtils.isMainThread());
            return super.remove(object);
        }

        @Override
        public boolean removeAll(Collection<?> collection) {
            Assert.assertTrue(MblUtils.isMainThread());
            return super.removeAll(collection);
        }

        @Override
        protected void removeRange(int fromIndex, int toIndex) {
            Assert.assertTrue(MblUtils.isMainThread());
            super.removeRange(fromIndex, toIndex);
        }
    };

    private LayoutInflater mLayoutInflater;

    /**
     * <pre>
     * Get LayoutInflater instance which is mandatory for most Adapter.
     * </pre>
     */
    protected LayoutInflater getLayoutInflater() {
        if (mLayoutInflater == null) {
            mLayoutInflater = LayoutInflater.from(MblUtils.getCurrentContext());
        }
        return mLayoutInflater;
    }

    /**
     * <pre>
     * Subclasses use this method to get internal data objects.
     * </pre>
     * @return list of internal data objects.
     */
    protected List<T> getData() {
        return mData;
    }

    @Override
    public void notifyDataSetChanged() {
        MblUtils.executeOnMainThread(new Runnable() {
            @Override
            public void run() {
                MblBaseAdapter.super.notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int pos) {
        if (pos < mData.size()) {
            return mData.get(pos);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

    /**
     * <pre>
     * Subclasses is strongly recommended to use this method when it wants to change internal data.
     * 
     * Here is an example:
     * <code>
     * public void appendData(Object newItem) {
     *     changeDataSafely(new Runnable() {
     *         {@literal @}Override
     *         public void run() {
     *            getData().add(newItem);
     *            notifyDataSetChanged();
     *         }
     *     });
     * }
     * </code>
     * </pre> 
     * @param action wraps all changes on internal data
     */
    protected void changeDataSafely(final Runnable action) {
        MblUtils.executeOnMainThread(new Runnable() {
            @Override
            public void run() {
                synchronized (mData) {
                    action.run();
                }
            }
        });
    }

    /**
     * <pre>
     * Discard all current data and replace them by new ones.
     * Also refresh UI automatically.
     * </pre>
     * @param data new data
     */
    public void changeData(final List<T> data) {
        changeDataSafely(new Runnable() {
            @Override
            public void run() {
                getData().clear();
                if (data != null) {
                    getData().addAll(data);
                }
                notifyDataSetChanged();
            }
        });
    }
    
    /**
     * <pre>
     * Discard all current data and replace them by new ones.
     * Also refresh UI automatically.
     * </pre>
     * @param data new data
     */
    public void changeData(final T[] data) {
        changeDataSafely(new Runnable() {
            @Override
            public void run() {
                getData().clear();
                if (data != null) {
                    for (T d : data) {
                        getData().add(d);
                    }
                }
                notifyDataSetChanged();
            }
        });
    }
}




Java Source Code List

com.datdo.mobilib.api.DBBase.java
com.datdo.mobilib.api.DBHelper.java
com.datdo.mobilib.api.MblApi.java
com.datdo.mobilib.api.MblCache.java
com.datdo.mobilib.api.MblException.java
com.datdo.mobilib.api.MblSSLCertificateUtils.java
com.datdo.mobilib.base.MblActivityPlugin.java
com.datdo.mobilib.base.MblBaseActionBarActivity.java
com.datdo.mobilib.base.MblBaseActivity.java
com.datdo.mobilib.base.MblBaseAdapter.java
com.datdo.mobilib.base.MblBaseApplication.java
com.datdo.mobilib.base.MblBaseFragmentActivity.java
com.datdo.mobilib.base.MblDecorView.java
com.datdo.mobilib.base.MblNetworkStatusChangedReceiver.java
com.datdo.mobilib.event.MblCommonEvents.java
com.datdo.mobilib.event.MblEventCenter.java
com.datdo.mobilib.event.MblEventListener.java
com.datdo.mobilib.event.MblStrongEventListener.java
com.datdo.mobilib.event.MblWeakArrayList.java
com.datdo.mobilib.imageinput.MblAutoResizeSquareImageView.java
com.datdo.mobilib.imageinput.MblDataInputActivity.java
com.datdo.mobilib.imageinput.MblImageFolderScanner.java
com.datdo.mobilib.imageinput.MblImageInput.java
com.datdo.mobilib.imageinput.MblImagePickingScanEngine.java
com.datdo.mobilib.imageinput.MblPickImageActivity.java
com.datdo.mobilib.imageinput.MblPickImageGridViewAdapter.java
com.datdo.mobilib.imageinput.MblTakeImageActivity.java
com.datdo.mobilib.util.MblAsyncTask.java
com.datdo.mobilib.util.MblImageLoader.java
com.datdo.mobilib.util.MblLinkMovementMethod.java
com.datdo.mobilib.util.MblUrlRecognizer.java
com.datdo.mobilib.util.MblUtils.java
com.datdo.mobilib.widget.MblHorizontalViewPager.java
com.datdo.mobilib.widget.MblListViewWithScrollableItems.java
com.datdo.mobilib.widget.MblSequenceImage.java
com.datdo.mobilib.widget.MblSideMenuEnabledLayout.java
com.datdo.mobilib.widget.MblTouchImageView.java