Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Android Mobility: Open Source Hits the Road : Page 3

Learn to leverage Android's powerful APIs to rapidly create sophisticated applications for media, data storage, and networking.


advertisement
Stick Around
The application won't be very useful if it only saves a single picture. You need to create a database that will keep track of all the pictures your users will take, as well as their associated information—things like their creation time and captions.

Google's Notepad tutorial application includes a sample wrapper, which wraps around a SQLite database called NotesDbAdapter. This is a good foundation for your application's database needs: it provides the power of SQL with a friendly API. The code shown in Listing 3 affords the same convenience.

With this adapter in hand, you can easily save a record of the picture:



CameraDbAdapter adapter = new CameraDbAdapter(Camera.this); adapter.open(); adapter.createImage(filename, "Enter a caption.", System.currentTimeMillis());

Photo Album
You now have an application that saves and stores pictures on the device. To make it more useful, what about providing a way to review the pictures that have been taken?

To display the items as a simple list, you can extend ListActivity and have it manage your UI. All you need is to define what each line item should look like. This happens with a single element in /res/layout/camera_item.xml:

<TextView android:id="@+id/albumItemCaption" xmlns:android="http://schemas.android.com/apk/res/android" android:textSize="20dip" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

The corresponding Album Activity is simple as well. With the database keeping track of your content and the ListActivity handling your interface, all you need is a little glue to tie those pieces together. That glue comes in the form of an Adapter. For a basic UI, the SimpleCursorAdapter will do the trick:

CameraDbAdapter adapter = new CameraDbAdapter(this); adapter.open(); SimpleCursorAdapter labels = new SimpleCursorAdapter (this, R.layout.camera_item, adapter.fetchAllImages(), new String[]{CameraDbAdapter.KEY_CAPTION}, new int[]{R.id.albumItemCaption}); setListAdapter(labels)

The parameters in the SimpleCursorAdapter bind the UI layout elements to the appropriate Cursor data. That's all there is to it! You could further customize the list by modifying the albumItemCaption XML element.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap