Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

2.7. Adapters

When you have to feed data from a data source to a view, you’ll use an Adapter, as we’ve seen. As the name suggests, an Adapter adapts a certain data source and hence lets you plug in different kinds of data sources into a view (an AdapterView) that can then render this data to the screen. ListView and Spinner are AdapterView views. Android ships with several predefined adapters, most notably ArrayAdapter, for serving data from a Java array object or Collection, and CursorAdapter for fetching data from a SQLite database (we’ll learn more about databases and cursors in chapter 7). You’re by no means restricted to the built-in adapters; you can, for instance, implement an adapter that wraps a web service and fetches data from the Internet directly into your views. Anything’s possible!

2.7.1. Adapter basics

The most basic way to use an adapter is to leverage one of the existing implementations Android provides, such as ArrayAdapter (which, despite the name, also works with collections). To see how this works, let’s take a quick look back at how we provided data for our Spinner in listing 2.5:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint