public class

ItemAdapter

extends BaseAdapter
java.lang.Object
   ↳ android.widget.BaseAdapter
     ↳ greendroid.widget.ItemAdapter

Class Overview

A ListAdapter that acts like an ArrayAdapter. An ItemAdapter manages a ListView that is backed by an array of Items. This is more advanced than a simple ArrayAdapter because it handles different types of itemviews internally. Adding, removing items from the internal array is also possible.

The ListView can be notified of underlying data changes manually using notifyDataSetChanged or automatically using the setNotifyOnChange(boolean) method.

Finally, an ItemAdapter can be created via XML code using the createFromXml(Context, int) method. This is a very powerful feature when you want to display static data or if you want to pre-populate your ItemAdapter.

Summary

[Expand]
Inherited Constants
From interface android.widget.Adapter
Public Constructors
ItemAdapter(Context context)
Constructs an empty ItemAdapter.
ItemAdapter(Context context, Item[] items)
Constructs an ItemAdapter using the specified items.
ItemAdapter(Context context, Item[] items, int maxViewTypeCount)
Constructs an ItemAdapter using the specified items.
ItemAdapter(Context context, List<Item> items)
Constructs an ItemAdapter using the specified items.
ItemAdapter(Context context, List<Item> items, int maxViewTypeCount)
Constructs an ItemAdapter using the specified items.
Public Methods
void add(Item item)
Adds the specified object at the end of the array.
void clear()
Remove all elements from the list.
static ItemAdapter createFromXml(Context context, int xmlId)
Creates an ItemAdapter from a given resource ID
static ItemAdapter createFromXml(Context context, XmlPullParser parser)
Creates an ItemAdapter from a given XML document.
int getActualViewTypeCount()
Returns the current number of different views types used in this ItemAdapter.
Context getContext()
Returns the context associated with this array adapter.
int getCount()
Object getItem(int position)
long getItemId(int position)
int getItemViewType(int position)
View getView(int position, View convertView, ViewGroup parent)
int getViewTypeCount()
void insert(Item item, int index)
Inserts the specified object at the specified index in the array.
boolean isEnabled(int position)
void remove(Item item)
Removes the specified object from the array.
void setNotifyOnChange(boolean notifyOnChange)
Control whether methods that change the list (add(Item), insert(Item, int), remove(Item), clear()) automatically call notifyDataSetChanged().
void sort(Comparator<? super Item> comparator)
Sorts the content of this adapter using the specified comparator.
[Expand]
Inherited Methods
From class android.widget.BaseAdapter
From class java.lang.Object
From interface android.widget.Adapter
From interface android.widget.ListAdapter
From interface android.widget.SpinnerAdapter

Public Constructors

public ItemAdapter (Context context)

Constructs an empty ItemAdapter.

Parameters
context The context associated with this array adapter.

public ItemAdapter (Context context, Item[] items)

Constructs an ItemAdapter using the specified items.

Note: Using this constructor implies the internal array will be immutable. As a result, adding or removing items will result in an exception.

Parameters
context The context associated with this array adapter.
items The array of Items use as underlying data for this ItemAdapter

public ItemAdapter (Context context, Item[] items, int maxViewTypeCount)

Constructs an ItemAdapter using the specified items.

Note: Using this constructor implies the internal array will be immutable. As a result, adding or removing items will result in an exception.

Note: A ListAdapter doesn't handle variable view type count (even after a notifyDataSetChanged). An ItemAdapter handles several types of cell are therefore use a trick to overcome the previous problem. This trick is to fool the ListView several types exist. If you already know the number of item types you'll possibly have, simply set it using this method

Parameters
context The context associated with this array adapter.
items The array of Items use as underlying data for this ItemAdapter
maxViewTypeCount The maximum number of view type that may be generated by this ItemAdapter

public ItemAdapter (Context context, List<Item> items)

Constructs an ItemAdapter using the specified items.

Parameters
context The context associated with this array adapter.
items The list of Items used as data for this ItemAdapter

public ItemAdapter (Context context, List<Item> items, int maxViewTypeCount)

Constructs an ItemAdapter using the specified items.

Note: A ListAdapter doesn't handle variable view type count (even after a notifyDataSetChanged). An ItemAdapter handles several types of cell are therefore use a trick to overcome the previous problem. This trick is to fool the ListView several types exist. If you already know the number of item types you'll possibly have, simply set it using this method

Parameters
context The context associated with this array adapter.
items The list of Items used as data for this ItemAdapter
maxViewTypeCount The maximum number of view type that may be generated by this ItemAdapter

Public Methods

public void add (Item item)

Adds the specified object at the end of the array.

Parameters
item The object to add at the end of the array.

public void clear ()

Remove all elements from the list.

public static ItemAdapter createFromXml (Context context, int xmlId)

Creates an ItemAdapter from a given resource ID

Parameters
context The Context in which the ItemAdapter will be used in
xmlId The resource ID of an XML file that describes a set of Item
Returns
  • a new ItemAdapter constructed with the content of the file pointed by xmlId
Throws
XmlPullParserException
IOException
XmlPullParserException

public static ItemAdapter createFromXml (Context context, XmlPullParser parser)

Creates an ItemAdapter from a given XML document. Called on a parser positioned at a tag in an XML document, tries to create an ItemAdapter from that tag.

Parameters
context The Context in which the ItemAdapter will be used in
parser The XmlPullParser
Returns
  • a new ItemAdapter constructed with the content of the file pointed by xmlId
Throws
XmlPullParserException
IOException
XmlPullParserException

public int getActualViewTypeCount ()

Returns the current number of different views types used in this ItemAdapter. Having a getCurrentViewTypeCount equal to getViewTypeCount means you won't be able to add a new type of view in this adapter (The Adapter class doesn't allow variable view type count).

Returns
  • The current number of different view types

public Context getContext ()

Returns the context associated with this array adapter. The context is used to create Views from the resource passed to the constructor.

Returns
  • The Context associated to this ItemAdapter

public int getCount ()

public Object getItem (int position)

public long getItemId (int position)

public int getItemViewType (int position)

public View getView (int position, View convertView, ViewGroup parent)

public int getViewTypeCount ()

public void insert (Item item, int index)

Inserts the specified object at the specified index in the array.

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

public boolean isEnabled (int position)

public void remove (Item item)

Removes the specified object from the array.

Parameters
item The object to remove.

public void setNotifyOnChange (boolean notifyOnChange)

Control whether methods that change the list (add(Item), insert(Item, int), remove(Item), clear()) automatically call 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.

Parameters
notifyOnChange if true, modifications to the list will automatically call notifyDataSetChanged()

public void sort (Comparator<? super Item> comparator)

Sorts the content of this adapter using the specified comparator.

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