Friday, February 11, 2011

Customized ListView Data Display in Android

ListViews are very handy user interface tools on the Android platform. We see them all the time in applications: phone number lists, contact name lists, shopping lists, all kinds of lists. The example Notepad application for Android, arguably the second app that all fledgling developers build is—you guessed it—a list.

Android provides some easy to use tools for mapping data from your application’s database into a ListView. However, you can quickly reach the limit of the basic tools as soon as you try to move past simply displaying data as it appears in your database. In this article, I discuss how to go beyond basic display of data in ListViews.

Lists, Cursors and the SimpleCursorAdapter

The SimpleCursorAdapter provides a simple way to map database fields into an entry in a List. All you have to do is provide a Cursor to your database table with the fields you want to display, map the fields to ids in your list item layout xml file and you are done:

       // Get all of the items from the database and create a list
       Cursor cursor = mDbHelper.fetchAllItems();
       startManagingCursor(cursor);
      
       // Create a mapping between database columns and layout fields
       String[] from = new String[] { DbAdapter.COL_TITLE };
       int[] to = new int[] {R.id.text1};
      
       // Create a cursor adapter and set it to display using a row layout
       SimpleCursorAdapter notes =
              new SimpleCursorAdapter(this, R.layout.list_item, cursor,
                                        from, to);

       setListAdapter(notes); // assumes this is a ListArray class

This approach works great for a basic list of text items or numbers, but what if you want to do something beyond just displaying database data as-is? Consider the following list of things you might want to do when displaying your data in a List:
  • Format the data for display
  • Optionally display data for each list item (skip display of some items)
  • Combine data from multiple data fields into a single display field
SimpleCursorAdapter does not allow for these variations, but hey, what did you expect? It is called SimpleCursorAdapter for a reason and for basic mapping it’s a perfect solution.

ComplexCursorAdapter?

So what is the next step up? No, not ComplexCursorAdapter; I made that up. The next step is to extend either the CursorAdapter or ResourceCursorAdapter classes. For this tutorial, I’ll focus on the use of ResourceCursorAdapter, which provides the ability to use XML-based layout resources for your list items.

Getting Started

This tutorial assumes you have already successfully created a few Android applications. So, if you have not done that yet, get Eclipse and the Android SDK, build Hello Android and the Notepad example applications and come back.

Create a new Android project with a ListActivity as the primary Activity for the application. If you want to follow along with my example project, you must choose Android SDK version 2.2 (8) or higher as your build target. After the Android Project Wizard creates the template Activity class, modify it to extend ListActivity (and make sure to update your imports):

public class CustomDataList extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Next, modify the default main.xml layout to include a ListView. (See the example project for an example.) If you are using your own layout, make sure your ListView includes the default id for lists:

    <ListView
              android:id="@android:id/list" ...

Create a layout xml for your list items under the ProjectName > res > layout. Make sure your layout has at least two TextView fields and make a note of their ids. If you need something to get started, check out the list_item_with_description.xml in the example project.

In your ListActivity class, create a new inner class that extends ResourceCursorAdapter. This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item:

    private class MyListAdapter extends ResourceCursorAdapter {
       
        public MyListAdapter(Context context, Cursor cursor) {
            super(context, R.layout.list_item_with_description, cursor);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // TODO Auto-generated method stub
        }
    }

The first method is the constructor, where we declare which list item layout to use. The bindView method is where the action happens. In this method, we take control over how data from our database gets populated into each list item.

Got Data?

In order to do data binding, we need data. So, use the DbAdapter class in the example project for sample data (recommended) or use a database adapter from one of your applications. If you use your own database adapter, make sure you have a method that returns a Cursor object with three or more data fields. In the example DbAdapter class, this method is called fetchListItems().

Connecting the Custom ListAdapter

Now we have a database and method to get a Cursor to our data, so it is time to connect the data pipes. In your ListActivity class, add the following to the onCreate() method:

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        DbAdapter dbHelper = new DbAdapter(this);
        dbHelper.open();
       
        // Get a Cursor for the list items
        Cursor listCursor = dbHelper.fetchListItems();
        startManagingCursor(listCursor);
        
        // set the custom list adapter
        setListAdapter(new MyListAdapter(this, listCursor));
    }

These statements create a connection to your database, get a Cursor to your list data and then set the customized ListAdapter, which tells the ListView how to render your data. Actually, we have not defined how the data is rendered for each list item yet; That is our next step.

Note: If you are getting a “method setListAdapter() is undefined” error, make sure the enclosing class extends ListActivity. This method is not available in a regular Activity class.

Binding it Your Way

With the implementation so far, no data is shown in our list! Now it is time to fix that problem by defining a data binding in the bindView() method of our custom ResourceCursorAdapter inner class. Here is how we update bindView() to render the data from the example database:

@Override
public void bindView(View view, Context context, Cursor cursor) {

       TextView title = (TextView) view.findViewById(R.id.item_title);
       title.setText(cursor.getString(
                           cursor.getColumnIndex(DbAdapter.COL_TITLE)));
      
       TextView details = (TextView) view.findViewById(
                                            R.id.item_details);
       StringBuffer detailsText = new StringBuffer();
      
       int price = cursor.getInt(cursor.getColumnIndex(                                            DbAdapter.COL_PRICE));

       if (price > 0){
              detailsText.append("$"+price+".00");
       } else {
              detailsText.append("Price Unavailable");
       }
       String description = cursor.getString(cursor.getColumnIndex(
                                             DbAdapter.COL_DESCRIPTION));
       if (description != null && description.length() > 0){
              detailsText.append(", "+description);
       }
       details.setText(detailsText.toString());
      
}

As you can see from this example, while we can still do a simple mapping of an item title data to a TextView, we also have the opportunity to do apply any rendering logic we want to the data retrieved from our database. A word of caution, however, is in order. Since the bindView() code is executed fairly frequently, it is a good idea to keep your rendering logic to a minimum, otherwise your users may experience significant pauses as your list renders.

Code Download


Here is the example project for this tutorial. You need Eclipse and the Android SDK with platform version 2.2 (8) to be able to run this project.

CustomDataList_CodeProject.zip

Conclusion

I hope you have found this article to be a useful next step in the evolution of your understanding of working with ListViews, Cursors and databases in Android. Please add your comments and questions below. Happy programming!

Wednesday, February 9, 2011

Managing Date and Time Data in Android Apps

The Android platform offers many well-defined options for managing various types of data in your applications. However, date and time values, such as in January 1, 2011 at 12:00pm, are an example of a data type on Android where you have many options, but the best path for storing and displaying them is not clear. This article discusses my investigation into managing date and time values for one of my Android applications, my conclusions and the approach I finally implemented.

Date and Times in SQLite

SQLite, the embedded database library in the Android platform, does not have a native date and time storage type (see SQLite docs), and instead suggests the use of TEXT, REAL or INTEGER data types. The library also has functions for working with dates and times, but I prefer to do my date and time calculations in Java, so these offerings were not of much use to me and did not figure into my decision about how to store this type of data.

Text or Numbers?

So, what data type to use? Text strings or number values? Text strings are convenient for display, but they are a pain when it comes time to do calculations (is this date more recent than that date?). In addition, if I ever want to do localized date display (European Day, Month Year format versus US Month / Day / Year), having dates preformatted for one locale means quite a bit of work to transform it into other locales. So, text strings get tossed out as a storage type!

That leaves storing dates and times as numbers. The big question there, is how precise do these dates and times need be? The SQLite INTEGER type can handle values up to 8 bytes (64 bits) in size. Using Java’s long value for time in milliseconds from January 1, 1970 might be convenient. However, I tossed out this idea because of concerns about the size of the numbers generated by this method and the fact that they are completely unreadable as dates without programming assistance. My application only needed precision down to seconds and I wanted the numbers to be as human-readable as possible without actually being formatted strings.

What’s Your Number?

With all this in mind, I decided to define a date and time number format that was closely related to printed dates and times, but that could be easily sorted into more recent and less recent. The format I defined starts with a numbered year, then month, then day of month, hours in 24 hour-format, minutes and then seconds.

Example Date/Time:   10/29/2007, 12:06am, 21 seconds
Date/Time Number:    20071029000621

Dates and times stored in this format can be interpreted by humans with a little instruction and the values will sort nicely, with higher values being the most recent.

Note: If you need to do length calculations in a specific unit (how many days between date X and date Y) this format isn’t much help, but unless you save your date and time values in that specific unit (e.g., days) doing those types of calculations is always going to be a bit of a pain.

The Implementation

With this design in mind, I created the following functions to help me convert between the Calendar objects and the long values I was going to retrieve out of my SQLite database:

    public static final String DATE_FORMAT = "yyyyMMddHHmmss";
    private static final SimpleDateFormat dateFormat = new
       SimpleDateFormat(DATE_FORMAT);
   
    public static long formatDateAsLong(Calendar cal){
       return Long.parseLong(dateFormat.format(cal.getTime()));
    }
   
    public static Calendar getCalendarFromFormattedLong(long l){
       try {
                     Calendar c = Calendar.getInstance();
                     c.setTime(dateFormat.parse(String.valueOf(l)));
                     return c;
                    
              } catch (ParseException e) {
                     return null;
              }
    }

This implementation uses Java’s SimpleDateFormat object to convert Calendar objects formatted long dates and formatted long dates back to Calendar objects.

Note: For those of you who might be wondering why I’m using a Calendar object as the primary Java data type for my date/times (rather than the Date object), it is because the implementation of Android’s DatePickerDialog is more closely aligned with the Calendar object.

In the database adapter for my application, the create and insert functions that include date and time information take a Calendar object and convert to a long just prior to inserting it into the SQLite database:

    public long updateDateRecord(long id, Calendar calendar){
        ContentValues values = new ContentValues();

        // Here is where we apply the Data DateFormat:
        values.put(COL_DATE, DateDataFormat
           .formatDateAsLong(calendar));

       
        return mDb.update(TABLE_DATES, values, COL_ID + "=" + id, null);
    }

On the user interface side, I retrieve the date and time value as a long, convert it to a Calendar object and then format the date into a string for display:

    private void populateDate(){
       String date = null;
       if (mListCursor != null && mListCursor.getCount() > 0){
             
              dateId = mListCursor.getLong(
                    mListCursor.getColumnIndex(DbAdapter.COL_ID));
              long d = mListCursor.getLong(
                    mListCursor.getColumnIndex(DbAdapter.COL_DATE));

              if (d > 0){
                  mDate = DateDataFormat
                      .getCalendarFromFormattedLong(d);

                          
                     date = DateDisplayFormat.getFormattedDate(mDate);
              }            
       }
       if (date != null){
              mDateText.setText(date);
       }
    }

Code Download

To review a complete, example implementation of this approach in an Android project, please download and import the following Android Eclipse project:

DateData_CodeProject.zip

You must have Eclipse and the Android SDK including platform version 1.5 (3) installed to compile and run this project.

Roads not Taken

Here are some things that were not considered in this approach, but might be relevant to your application:

  • Time Zones – All the date and times I was recording for my application were in relation to one user in a single time zone, so they did not figure into my implementation. Folks working with dates and times that need to be coordinated across time zones would need to enhance my approach using an implementation of Coordinated Universal Time (UTC).
  • High-Precision Times – All the dates and times for my application would differ by at least a second or more, so there was no reason for me to include millisecond precision.

Conclusion

This example implementation is not meant to be the ultimate solution for saving and retrieving date and time data in Android. As noted above, your application may have different needs which are not considered by this methodology, which you should seriously consider before adopting this approach.

I hope this article helps you more easily navigate the decisions of date and time storage on the Android platform and gets you another step further in completing your app. Happy programming!

Tuesday, December 21, 2010

Blinky Christmas List v1.0.3 Released!

We released version 1.0.3 of Blinky Christmas List is released today!


This release is a recommended upgrade for current users, since it fixes a gift list-sync problem between the Gifts tab and the Shopping tab. Users should also see a modest performance improvement.


Scan the bar code on the right with Google Goggles to go directly to Blinky Christmas List in the Android Market, or search for "Blinky" or "Christmas List" in the Market app.


Be sure to rate Blinky Christmas List and comment on it!

Friday, December 17, 2010

My First Sale!

December 17, 2010 7:19am - A historic moment in my Android app-publishing career: My first sale of Blinky Christmas List! I'm so proud!

Thursday, December 16, 2010

Blinky Christmas List

My brand new Android application Blinky Christmas List is now live on the Android Market!


Here are few screenshots:


Features:


  • People View: Create shopping list of people, then add gifts, or...
  • Gift List View: List gifts first, then assign to people
  • Shopping List view: click, bought, done!
  • Edit People details (long click); Have they been naughty or nice?
  • Edit Gift details (long click); Purchased, Price and extra notes
  • Plus soothing, blinking Christmas lights keep up your holiday spirits!


If you are browsing this page on your computer and have your Android phone handy, you can use the QR code on the right to go direct to Blinky Christmas List in the Android Market. Just power up Google Googles on your phone, snap a picture of the matrix bar code on the right and follow the link.


If you are browsing this page from an Android phone, you can click this link to go right to the app: http://market.android.com/details?id=com.jaa.xmlist Warning! This link does not work in a regular web browser!


Otherwise, to get to Blinky Christmas List on the Android Market, open the Market application on your Android phone and search for "Blinky" or "Christmas List".


Let me know what you think!


\\J

Saturday, December 11, 2010

Watch this Space!

I'm about to release my first app on the Android Market! Can't talk about it right now. Top secret, you know. I will say that it's, aahh, seasonal.

Anyway, can't talk! Busy, busy, busy! So much to do!