This is a mirror of official site: http://jasper-net.blogspot.com/

Android Fundamentals: Working With Content Providers

| Thursday, April 7, 2011
The TutList application that we’ve been working with has a pretty big flaw right now: the article data is not “live”, but static content. In this tutorial, you take several more steps towards a flexible and expandable solution by modifying the application to act as a data-backed content provider.

The Android framework uses a concept called content providers to enable applications to share and use data across the platform. Typically, a provider is backed by a SQLite database where the underlying data is stored. The current state of the TutList application is such that it gets the data for the ListView from static string arrays in the resources. In this tutorial, we’ll remove those fixed data resources and build a flexible database-driven content provider in their place. You’ll see that the user interface code won’t change much. The back-end, however, will get more complex. The advantage here is that when we finally switch the application over to retrieving live data from the Internet, we’ll have a place to store and manage it easily.
The pacing of this tutorial will be faster than some of our beginner tutorials; you may have to review some of the other Android tutorials on this site or even in the Android SDK reference if you are unfamiliar with any of the basic Android concepts and classes discussed in this tutorial. You can read the SQLite Crash Course for Android Developers tutorial to refresh your SQLite knowledge. The final sample code that accompanies this tutorial is available for download as open-source from the Google code hosting.

Step 0: Getting Started
This tutorial assumes you will start where our last tutorial, Android Compatibility: Working with Fragments, left off. You can download that code and build from there or you can download the code for this tutorial and follow along. Either way, get ready by downloading one or the other project and importing it into Eclipse.

Step 1: Creating the Database Class
First, you must create the application’s underlying SQLite database. Begin by creating a new class named TutListDatabase that extends from SQLiteOpenHelper. We placed it in the com.mamlambo.tutorial.tutlist.data package to separate it out from the user interface portion of the app. While you’re at it, define the database configuration information in the class, such as the name of the database and its version number. We’ve done this with constants. Finally, use update the TutListDatabase class constructor to reference these values, as follows:

public class TutListDatabase extends SQLiteOpenHelper {
    private static final String DEBUG_TAG = "TutListDatabase";
    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "tutorial_data";
 
    public TutListDatabase(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
 
    @Override
    public void onCreate(SQLiteDatabase db) {
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

Read more: Mobile tuts+

Posted via email from Jasper-net

0 comments: