Typically the user interacts with the list via the action bar, for example, via a refresh button.
Android provides the ListView
and the ExpandableListView
classes which is capable of displaying a scrollable list of items.
The input to the list (items in the list) can be arbitrary Java objects. The adapter extracts the correct data from the data object and assigns this data to the views in the row of the ListView
.
An adapter manages the data model and adapts it to the individual rows in the list view. An adapter extends theBaseAdapter
class.
The adapter would inflate the layout for each row in its getView()
method and assign the data to the individual views in the row.
The notifyDataSetChanged()
method on the adapter is called if the data has changed or if new data is available.
The notifyDataSetInvalidated()
method is called if the data is not available anymore.
Default Adapter
the most important are ArrayAdapter
and CursorAdapter
.
ArrayAdapter
can handle data based on Arrays
or java.util.List
.
SimpleCursorAdapter
can handle database related data.
final ListView listview = (ListView) findViewById(R.id.listview); String[] values = new String[] { "Android", "iPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2", "Android", "iPhone", "WindowsMobile" }; final ArrayList<String> list = new ArrayList<String>(); for (int i = 0; i < values.length; ++i) { list.add(values[i]); } final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list); listview.setAdapter(adapter);
Custom Adapter
ArrayAdapter
is limited as it supports only the mapping of toString()
to one view in the row layout. To control the data assignment and to support several views, you have to create your custom adapter implementation.