You are previewing Learning Android.

Learning Android

Cover of Learning Android by Marko Gargenta Published by O'Reilly Media, Inc.
  1. Learning Android
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. What’s Inside
      2. Conventions Used in This Book
      3. Using Code Examples
      4. Safari® Books Online
      5. How to Contact Us
      6. Acknowledgments
    4. 1. Android Overview
      1. Android Overview
      2. History
      3. Android Versions
      4. Summary
    5. 2. The Stack
      1. Stack Overview
      2. Linux
      3. Native Libraries
      4. Dalvik
      5. Application Framework
      6. Applications
      7. Summary
    6. 3. Quick Start
      1. Installing the Android SDK
      2. Hello, World
      3. The Emulator
      4. Summary
    7. 4. Main Building Blocks
      1. What Are Main Building Blocks?
      2. A Real-World Example
      3. Activities
      4. Intents
      5. Services
      6. Content Providers
      7. Broadcast Receivers
      8. Application Context
      9. Summary
    8. 5. Yamba Project Overview
      1. The Yamba Application
      2. Design Philosophy
      3. Project Design
      4. Part 1: Android User Interface
      5. Part 2: Preferences, Filesystem, Options Menu, and Intents
      6. Part 3: Android Services
      7. Part 4: Working with Databases
      8. Part 5: Lists and Adapters
      9. Part 6: Broadcast Receivers
      10. Part 7: Content Providers
      11. Part 8: System Services
      12. Summary
    9. 6. Android User Interface
      1. Two Ways to Create a User Interface
      2. Views and Layouts
      3. Starting the Yamba Project
      4. The StatusActivity Layout
      5. The StatusActivity Java Class
      6. Logging in Android
      7. Threading in Android
      8. Other UI Events
      9. Adding Color and Graphics
      10. Alternative Resources
      11. Optimizing the User Interface
      12. Summary
    10. 7. Preferences, the Filesystem, the Options Menu, and Intents
      1. Preferences
      2. The Options Menu
      3. Shared Preferences
      4. The Filesystem Explained
      5. Summary
    11. 8. Services
      1. The Yamba Application Object
      2. UpdaterService
      3. Looping in the Service
      4. Pulling Data from Twitter
      5. Summary
    12. 9. The Database
      1. About SQLite
      2. DbHelper
      3. First Example
      4. Update UpdaterService
      5. Refactoring Status Data
      6. Summary
    13. 10. Lists and Adapters
      1. TimelineActivity
      2. Basic TimelineActivity Layout
      3. About Adapters
      4. TimelineAdapter
      5. ViewBinder: A Better Alternative to TimelineAdapter
      6. Updating the Manifest File
      7. Base Activity
      8. Summary
    14. 11. Broadcast Receivers
      1. About Broadcast Receivers
      2. BootReceiver
      3. The TimelineReceiver
      4. Broadcasting Intents
      5. The Network Receiver
      6. Adding Custom Permissions to Send and Receive Broadcasts
      7. Summary
    15. 12. Content Providers
      1. Creating a Content Provider
      2. Using Content Providers Through Widgets
      3. Summary
    16. 13. System Services
      1. Compass Demo
      2. Location Service
      3. Updating Yamba to Use the Location Service
      4. Intent Service
      5. Sending Notifications
      6. Summary
    17. 14. The Android Interface Definition Language
      1. Implementing the Remote Service
      2. Implementing the Remote Client
      3. Summary
    18. 15. The Native Development Kit (NDK)
      1. What Is and Isn’t the NDK For?
      2. Problems Solved by the NDK
      3. An NDK Example: Fibonacci
      4. Summary
    19. Index
    20. About the Author
    21. Colophon
    22. SPECIAL OFFER: Upgrade this ebook with O’Reilly
O'Reilly logo

About Adapters

A ScrollView will work for a few dozen records. But what if your status database has hundreds or even thousands of records? Waiting to get and print them all would be highly inefficient. The user probably doesn’t even care about all of the data anyhow.

To address this issue, Android provides adapters. These are a smart way to connect a View with some kind of data source (see Figure 10-1). Typically, your view would be a ListView and the data would come in the form of a Cursor or Array. So adapters come as subclasses of CursorAdapter or ArrayAdapter.

Adapter

Figure 10-1. Adapter

Adding a ListView to TimelineActivity

As before, our first stop in upgrading our applications is our resources file. We’ll add a ListView to the timeline layout by editing timeline.xml, shown in Example 10-3.

Example 10-3. res/layout/timeline.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_height="fill_parent"
  android:layout_width="fill_parent" android:background="@drawable/background">
  <TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_gravity="center"
    android:layout_margin="10dp" android:text="@string/titleTimeline"
    android:textColor="#fff" android:textSize="30sp" />

  <!--  --> <ListView android:layout_height="fill_parent" android:layout_width="fill_parent" ...

The best content for your career. Discover unlimited learning on demand for around $1/day.