ArrayAdapterpublic class ArrayAdapter extends BaseAdapter implements FilterableA 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 | mObjectsContains 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 | mLockLock 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 | mResourceThe resource indicating what views to inflate to display the content of this
array adapter. | private int | mDropDownResourceThe resource indicating what views to inflate to display the content of this
array adapter in a drop down widget. | private int | mFieldIdIf 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 | mNotifyOnChangeIndicates 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
init(context, resource, 0, new ArrayList<T>());
| public ArrayAdapter(android.content.Context context, int resource, int textViewResourceId)Constructor
init(context, resource, textViewResourceId, new ArrayList<T>());
| public ArrayAdapter(android.content.Context context, int resource, T[] objects)Constructor
init(context, resource, 0, Arrays.asList(objects));
| public ArrayAdapter(android.content.Context context, int resource, int textViewResourceId, T[] objects)Constructor
init(context, resource, textViewResourceId, Arrays.asList(objects));
| public ArrayAdapter(android.content.Context context, int resource, List objects)Constructor
init(context, resource, 0, objects);
| public ArrayAdapter(android.content.Context context, int resource, int textViewResourceId, List objects)Constructor
init(context, resource, textViewResourceId, objects);
|
Methods Summary |
---|
public void | add(T object)Adds the specified object at the end of the array.
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.add(object);
} else {
mObjects.add(object);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
| public void | addAll(java.util.Collection collection)Adds the specified Collection at the end of the array.
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.addAll(collection);
} else {
mObjects.addAll(collection);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
| public void | addAll(T items)Adds the specified items at the end of the array.
synchronized (mLock) {
if (mOriginalValues != null) {
Collections.addAll(mOriginalValues, items);
} else {
Collections.addAll(mObjects, items);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
| public void | clear()Remove all elements from the list.
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.clear();
} else {
mObjects.clear();
}
}
if (mNotifyOnChange) notifyDataSetChanged();
| public static android.widget.ArrayAdapter | createFromResource(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)}.
CharSequence[] strings = context.getResources().getTextArray(textArrayResId);
return new ArrayAdapter<CharSequence>(context, textViewResId, strings);
| private android.view.View | createViewFromResource(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.Context | getContext()Returns the context associated with this array adapter. The context is used
to create views from the resource passed to the constructor.
return mContext;
| public int | getCount(){@inheritDoc}
return mObjects.size();
| public android.view.View | getDropDownView(int position, android.view.View convertView, android.view.ViewGroup parent){@inheritDoc}
return createViewFromResource(position, convertView, parent, mDropDownResource);
| public Filter | getFilter(){@inheritDoc}
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
| public T | getItem(int position){@inheritDoc}
return mObjects.get(position);
| public long | getItemId(int position){@inheritDoc}
return position;
| public int | getPosition(T item)Returns the position of the specified item in the array.
return mObjects.indexOf(item);
| public android.view.View | getView(int position, android.view.View convertView, android.view.ViewGroup parent){@inheritDoc}
return createViewFromResource(position, convertView, parent, mResource);
| private void | init(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 void | insert(T object, int index)Inserts the specified object at the specified index in the array.
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.add(index, object);
} else {
mObjects.add(index, object);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
| public void | notifyDataSetChanged(){@inheritDoc}
super.notifyDataSetChanged();
mNotifyOnChange = true;
| public void | remove(T object)Removes the specified object from the array.
synchronized (mLock) {
if (mOriginalValues != null) {
mOriginalValues.remove(object);
} else {
mObjects.remove(object);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
| public void | setDropDownViewResource(int resource)Sets the layout resource to create the drop down views.
this.mDropDownResource = resource;
| public void | setNotifyOnChange(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.
mNotifyOnChange = notifyOnChange;
| public void | sort(java.util.Comparator comparator)Sorts the content of this adapter using the specified comparator.
synchronized (mLock) {
if (mOriginalValues != null) {
Collections.sort(mOriginalValues, comparator);
} else {
Collections.sort(mObjects, comparator);
}
}
if (mNotifyOnChange) notifyDataSetChanged();
|
|