14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5: Intents, Broadcast Receivers, Adapters, and the Internet<br />

136<br />

Android also uses Broadcast Intents to monitor application-specifi c events like incoming SMS messages.<br />

The actions and Intents associated with these events are discussed in more detail in later chapters<br />

when you learn more about the associated Services.<br />

Introducing Adapters<br />

Adapters are bridging classes that bind data to user-interface Views. The adapter is responsible for creating<br />

the child views used to represent each item and providing access to the underlying data.<br />

User-interface controls that support Adapter binding must extend the AdapterView abstract class. It’s<br />

possible to create your own AdapterView-derived controls and create new Adapter classes to bind them.<br />

Introducing Some Android-Supplied Adapters<br />

In many cases, you won’t have to create your own Adapter from scratch. Android supplies a set of<br />

Adapters that pump data into the native user-interface widgets.<br />

Because Adapters are responsible both for supplying the data and selecting the Views that represent each<br />

item, Adaptors can radically modify the appearance and functionality of the controls they’re bound to.<br />

The following list highlights two of the most useful and versatile native adapters:<br />

❑ ArrayAdapter The ArrayAdapter is a generic class that binds Adapter Views to an array of<br />

objects. By default, the ArrayAdapter binds the toString value of each object to a TextView<br />

control defi ned within a layout. Alternative constructors allow you to use more complex layouts,<br />

or you can extend the class to use alternatives to Text View (such as populating an<br />

ImageView or nested layout) by overriding the getView method.<br />

❑ SimpleCursorAdapter The SimpleCursorAdapter binds Views to cursors returned from<br />

Content Provider queries. You specify an XML layout defi nition and then bind the value within<br />

each column in the result set, to a View in that layout.<br />

The following sections will delve into these Adapter classes in more detail. The examples provided<br />

bind data to List Views, although the same logic will work just as well for other AdapterView classes<br />

such as Spinners and Galleries.<br />

Using Adapters for Data Binding<br />

To apply an Adapter to an AdapterView-derived class, you call the View’s setAdapter method, passing<br />

in an Adapter instance, as shown in the snippet below:<br />

ArrayList myStringArray = new ArrayList();<br />

ArrayAdapter myAdapterInstance;<br />

int layoutID = android.R.layout.simple_list_item_1;<br />

myAdapterInstance = new ArrayAdapter(this, layoutID,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!