Additionally, ListActivity offers a predefined method onItemClick() which is called when someone clicks on a list element. This method takes four arguments that allow us to access the selected element:
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Show info about selected item
Toast.makeText(view.getContext(), "Item's database ID: "+id+"\nPosition in list: "+position, Toast.LENGTH_SHORT).show();
}
});
When rendering a ListView, Android provides a default layout, as well as a selection of pre-made layouts for individual list items.Binding Data to the ListView
We can bind to any data source that's compatible with the ListAdapter class. In practice, this means either an ArrayAdapter or a CursorAdapter.Binding to Static Data in Arrays
Use the ArrayAdapter to convert an array into a form that can be attached to a ListView.Binding to Dynamic Data from a Database
We can attach the ListView to a cursor representing our result set by creating a CursorAdapter. Note that the cursor must contain the ID of each database row, as _id (but the ID should not be included in either the from or to arrays).Custom ListView Layouts
If we want, we can override this layout by creating an XML layout and passing its name to the setContentView() method. Any such custom layout must contain a ListView element with the id @android:id/list.Your XML layout can also define a control, e.g. a TextView, to be displayed when the list is empty. This control must have the id @android:id/empty.