Using PHP pspell Spell Check Functions with a Custom Dictionary
Getting Set up with Ogre 3D on Ubuntu
A Simple ISAPI Filter for Authentication on IIS
Nice n' Easy JQuery Image Rotator
Development Resource Project
Changing Mailman Python Scripts for Virtual Host Support

Binding Data to a ListActivity's ListView

Thursday, 29 September 11, 1:00 pm
Extending the ListActivity class is the only way Android applications can display data with a ListView widget. The ListActivity class contains a ListAdapter, which is how we attach our data to the ListView. This adapter must be set in the onCreate() method of your Activity via the method setListAdapter().

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.

Please enter your comment in the box below. Comments will be moderated before going live. Thanks for your feedback!

Cancel Post

/xkcd/ Metric Tip