Android Tutorial

(avery) #1

By : Ketan Bhimani


346 

We can then save our Pet Record as we have before.

Displaying Images Retrieved from the SD Card Using URIs

Now that our Pet Entry form is saved properly, we must turn our
attention to the Pet Listing screen. Our ListView is getting more
complicated; each item needs to contain an ImageView and two
TextView widgets for the pet name and species. We begin by
defining a custom layout template for each ListView item called
pet_item.xml. This should be familiar; it contains an ImageView
and two TextView objects.

We want to make sure this implementation is scalable, in case we
want to add new features to individual ListView items in the future.
So instead of taking shortcuts and using standard adapters and
built-in Android layout templates, we implement another custom
adapter called PetListAdapter.

The PetListAdapter is similar to the ImageUriAdapter we previously
implemented for the Gallery widget. This time, instead of Gallery
child items, we work with the ListView child records, which
correspond to each pet. Again, the constructor maps the Cursor
data to an array of PetRecord objects.

The getView() method of the PetListAdapter is where the magic
occurs. Here we use a LayoutInflater to inflate our custom layout
file called pet_item.xml for each ListView item. Again we use the
Tag property of the view to store any information about the record
that we might use later. It is here that we use the URI information
we stored in our database to rebuild the fully qualified image URI
using the Uri.parse() and ContentUris.withAppendedId() utility
methods and assign this URI to the ImageView widget using the
Free download pdf