Android Tutorial

(avery) #1
Android Tutorial 345

specific image within the Gallery. We save this information in our database
so that we know which image corresponds to which pet.
 The getView() method returns the custom View widget that corresponds to
each child View within the Gallery. In this case, we return an ImageView
with the corresponding image. We set each view’s Tag property to the
associated GalleryRecord object, which includes all our Cursor information
we mapped for that record. This is a nifty trick for storing extra information
with widgets for later use.

After all this magic has been implemented, we can set our newly
defined custom adapter to the adapter used by the Gallery with our
new Cursor.

ImageUriAdapter iAdapter = new ImageUriAdapter(this,
mCursorImages, mMedia);
final Gallery pictureGal = (Gallery)fndViewById(R.id.GalleryOfPics);
pictureGal.setAdapter(iAdapter);


Retrieving Gallery Images and Saving Them in the Database

Notice that we added two new columns to our SQLite database: the
base URI for the image and the individual image id, which is the
unique identifier tacked to the end of the URI. We do not save the
image itself in the database, only the URI information to retrieve it.

When the user presses the Save button on the Pet Entry screen, we
examine the Gallery item selected and extract the information we
require from the Tag property of the selected View, like this:

final Gallery gall = (Gallery) findViewById(R.id.GalleryOfPics);
ImageView selectedImageView = (ImageView) gall.getSelectedView();
GalleryRecord galleryItem;
if ( selectedImageView != null) {
galleryItem = (GalleryRecord)selectedImageView.getTag();
long imageId = galleryItem.getImageId();
String strImageUriPathString = galleryItem.getImageUriPath();
}

Free download pdf