FileDocCategorySizeDatePackage
ArrayAdapter.javaAPI DocAndroid 5.1 API17355Thu Mar 12 22:22:10 GMT 2015android.widget

ArrayAdapter

public class ArrayAdapter extends BaseAdapter implements Filterable
A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.

To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override {@link #getView(int, View, ViewGroup)} to return the type of view you want.

Fields Summary
private List
mObjects
Contains the list of objects that represent the data of this ArrayAdapter. The content of this list is referred to as "the array" in the documentation.
private final Object
mLock
Lock used to modify the content of {@link #mObjects}. Any write operation performed on the array should be synchronized on this lock. This lock is also used by the filter (see {@link #getFilter()} to make a synchronized copy of the original array of data.
private int
mResource
The resource indicating what views to inflate to display the content of this array adapter.
private int
mDropDownResource
The resource indicating what views to inflate to display the content of this array adapter in a drop down widget.
private int
mFieldId
If the inflated resource is not a TextView, {@link #mFieldId} is used to find a TextView inside the inflated views hierarchy. This field must contain the identifier that matches the one defined in the resource file.
private boolean
mNotifyOnChange
Indicates whether or not {@link #notifyDataSetChanged()} must be called whenever {@link #mObjects} is modified.
private android.content.Context
mContext
private ArrayList
mOriginalValues
private ArrayFilter
mFilter
private android.view.LayoutInflater
mInflater
Constructors Summary
public ArrayAdapter(android.content.Context context, int resource)
Constructor

param
context The current context.
param
resource The resource ID for a layout file containing a TextView to use when instantiating views.


                                                
         
        init(context, resource, 0, new ArrayList<T>());
    
public ArrayAdapter(android.content.Context context, int resource, int textViewResourceId)
Constructor

param
context The current context.
param
resource The resource ID for a layout file containing a layout to use when instantiating views.
param
textViewResourceId The id of the TextView within the layout resource to be populated

        init(context, resource, textViewResourceId, new ArrayList<T>());
    
public ArrayAdapter(android.content.Context context, int resource, T[] objects)
Constructor

param
context The current context.
param
resource The resource ID for a layout file containing a TextView to use when instantiating views.
param
objects The objects to represent in the ListView.

        init(context, resource, 0, Arrays.asList(objects));
    
public ArrayAdapter(android.content.Context context, int resource, int textViewResourceId, T[] objects)
Constructor

param
context The current context.
param
resource The resource ID for a layout file containing a layout to use when instantiating views.
param
textViewResourceId The id of the TextView within the layout resource to be populated
param
objects The objects to represent in the ListView.

        init(context, resource, textViewResourceId, Arrays.asList(objects));
    
public ArrayAdapter(android.content.Context context, int resource, List objects)
Constructor

param
context The current context.
param
resource The resource ID for a layout file containing a TextView to use when instantiating views.
param
objects The objects to represent in the ListView.

        init(context, resource, 0, objects);
    
public ArrayAdapter(android.content.Context context, int resource, int textViewResourceId, List objects)
Constructor

param
context The current context.
param
resource The resource ID for a layout file containing a layout to use when instantiating views.
param
textViewResourceId The id of the TextView within the layout resource to be populated
param
objects The objects to represent in the ListView.

        init(context, resource, textViewResourceId, objects);
    
Methods Summary
public voidadd(T object)
Adds the specified object at the end of the array.

param
object The object to add at the end of the array.

        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.add(object);
            } else {
                mObjects.add(object);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    
public voidaddAll(java.util.Collection collection)
Adds the specified Collection at the end of the array.

param
collection The Collection to add at the end of the array.

        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.addAll(collection);
            } else {
                mObjects.addAll(collection);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    
public voidaddAll(T items)
Adds the specified items at the end of the array.

param
items The items to add at the end of the array.

        synchronized (mLock) {
            if (mOriginalValues != null) {
                Collections.addAll(mOriginalValues, items);
            } else {
                Collections.addAll(mObjects, items);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    
public voidclear()
Remove all elements from the list.

        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.clear();
            } else {
                mObjects.clear();
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    
public static android.widget.ArrayAdaptercreateFromResource(android.content.Context context, int textArrayResId, int textViewResId)
Creates a new ArrayAdapter from external resources. The content of the array is obtained through {@link android.content.res.Resources#getTextArray(int)}.

param
context The application's environment.
param
textArrayResId The identifier of the array to use as the data source.
param
textViewResId The identifier of the layout used to create views.
return
An ArrayAdapter.

        CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
        return new ArrayAdapter<CharSequence>(context, textViewResId, strings);
    
private android.view.ViewcreateViewFromResource(int position, android.view.View convertView, android.view.ViewGroup parent, int resource)

        View view;
        TextView text;

        if (convertView == null) {
            view = mInflater.inflate(resource, parent, false);
        } else {
            view = convertView;
        }

        try {
            if (mFieldId == 0) {
                //  If no custom field is assigned, assume the whole resource is a TextView
                text = (TextView) view;
            } else {
                //  Otherwise, find the TextView field within the layout
                text = (TextView) view.findViewById(mFieldId);
            }
        } catch (ClassCastException e) {
            Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
            throw new IllegalStateException(
                    "ArrayAdapter requires the resource ID to be a TextView", e);
        }

        T item = getItem(position);
        if (item instanceof CharSequence) {
            text.setText((CharSequence)item);
        } else {
            text.setText(item.toString());
        }

        return view;
    
public android.content.ContextgetContext()
Returns the context associated with this array adapter. The context is used to create views from the resource passed to the constructor.

return
The Context associated with this adapter.

        return mContext;
    
public intgetCount()
{@inheritDoc}

        return mObjects.size();
    
public android.view.ViewgetDropDownView(int position, android.view.View convertView, android.view.ViewGroup parent)
{@inheritDoc}

        return createViewFromResource(position, convertView, parent, mDropDownResource);
    
public FiltergetFilter()
{@inheritDoc}

        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    
public TgetItem(int position)
{@inheritDoc}

        return mObjects.get(position);
    
public longgetItemId(int position)
{@inheritDoc}

        return position;
    
public intgetPosition(T item)
Returns the position of the specified item in the array.

param
item The item to retrieve the position of.
return
The position of the specified item.

        return mObjects.indexOf(item);
    
public android.view.ViewgetView(int position, android.view.View convertView, android.view.ViewGroup parent)
{@inheritDoc}

        return createViewFromResource(position, convertView, parent, mResource);
    
private voidinit(android.content.Context context, int resource, int textViewResourceId, java.util.List objects)

        mContext = context;
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mResource = mDropDownResource = resource;
        mObjects = objects;
        mFieldId = textViewResourceId;
    
public voidinsert(T object, int index)
Inserts the specified object at the specified index in the array.

param
object The object to insert into the array.
param
index The index at which the object must be inserted.

        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.add(index, object);
            } else {
                mObjects.add(index, object);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    
public voidnotifyDataSetChanged()
{@inheritDoc}

        super.notifyDataSetChanged();
        mNotifyOnChange = true;
    
public voidremove(T object)
Removes the specified object from the array.

param
object The object to remove.

        synchronized (mLock) {
            if (mOriginalValues != null) {
                mOriginalValues.remove(object);
            } else {
                mObjects.remove(object);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();
    
public voidsetDropDownViewResource(int resource)

Sets the layout resource to create the drop down views.

param
resource the layout resource defining the drop down views
see
#getDropDownView(int, android.view.View, android.view.ViewGroup)

        this.mDropDownResource = resource;
    
public voidsetNotifyOnChange(boolean notifyOnChange)
Control whether methods that change the list ({@link #add}, {@link #insert}, {@link #remove}, {@link #clear}) automatically call {@link #notifyDataSetChanged}. If set to false, caller must manually call notifyDataSetChanged() to have the changes reflected in the attached view. The default is true, and calling notifyDataSetChanged() resets the flag to true.

param
notifyOnChange if true, modifications to the list will automatically call {@link #notifyDataSetChanged}

        mNotifyOnChange = notifyOnChange;
    
public voidsort(java.util.Comparator comparator)
Sorts the content of this adapter using the specified comparator.

param
comparator The comparator used to sort the objects contained in this adapter.

        synchronized (mLock) {
            if (mOriginalValues != null) {
                Collections.sort(mOriginalValues, comparator);
            } else {
                Collections.sort(mObjects, comparator);
            }
        }
        if (mNotifyOnChange) notifyDataSetChanged();