[This web page is a work in progress.]
"A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view."
ListView reference:
http://developer.android.com/reference/android/widget/ListView.html
ListView is a widely used UI widget and can be implemented in a variety of ways - from simple to complex.
Main Steps
1) Create the data source
2) Create the ListView
3) Create the ArrayAdapter
3A) Create the ArrayAdapter using the new operator.
Parameters: context, built-in or customized ListView row layout, optional customized textView layout, array data source.
If you are using an XML string-array file, you cannot assign it directly into the array data source parameter.
You need to declare an array and pull the data from the resources using a statement such as this:
mStringArray = getResources().getStringArray(R.array.MyArray);
Now you can use your mStringArray as the ArrayAdapter array data source.
3B) Create the ArrayAdapter using the convenience factory method:
public static ArrayAdapter<CharSequence> createFromResource (Context context, int textArrayResId, int textViewResId);
Parameters
context The application's environment.
textArrayResId The identifier of the array to use as the data source.
textViewResId The identifier of the layout used to create views.
You can use the R file position of the XML array without using getResources() to create an array from the XML data in your code.
In my opinion, it will be easier to work with the XML data if you have it in an array.
4) Set the ArrayAdapter to the ListView
setListAdapter(adapter);
5) Handle user selections using the onListItemClick() method
protected void onListItemClick (ListView l, View v, int position, long id)
This method will be called by the system when an item in the list is selected. Subclasses should override. Subclasses can call getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
l The ListView where the click happened
v The view that was clicked within the ListView
position The position of the view in the list
id The row id of the item that was clicked
Note on Extending ListActivity
Use Cases
Data sources: 1) Java array, 2) XML resource, 3) SQLite database, 4) other.
List types: 1) Default, 2) With image, 3) customized using adapter subclass.
Optimized: Multi-threaded.
Examples
Java code array as data source