Using Adapters for Data Binding

To apply an Adapter to an AdapterView-derived class you call the View'setAdapter method, passing in an Adapter instance as shown in Listing 5-24.

LISTING 5-24: Creating and applying an Adapter Available for download on ArrayList<String> myStringArray = new ArrayList<String>(); ArrayAdapter<String> myAdapterlnstance;

int layoutID = android.R.layout.simple_list_item_1;

myAdapterInstance = new ArrayAdapter<String>(this, layoutID , myStringArray); myListView.setAdapter(myAdapterInstance);

This snippet shows the most simplistic case, in which the array being bound contains Strings and each List View item is represented by a single Text View.

The first of the following examples demonstrates how to bind an array of complex objects to a List View using a custom layout. The second shows how to use a Simple Cursor Adapter to bind a query result to a custom layout within a List View.

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment