CS 491 Lecture 6 – ListView and Adapters
Before class
A
Before our next class:
- Watch http://youtu.be/SA2VfGmsbOs.
- Read sections 1 and 3 of http://www.vogella.com/articles/AndroidListView/article.html. (Your partner’s working on 2.)
Here’s a brief outline of the video’s contents:
- A need for series data widgets
- Several: ListView, GridView, Spinner
- Create a String array
- TextToSpeech
- onItemClick to speak
- onLongItemClick to delete
- addHeaderView
- creating a TextView programmatically
B
Before our next class:
- Watch http://youtu.be/ikdr76gFcMI.
- Read http://mobile.cs.fsu.edu/the-nuance-of-android-listview-recycling-for-n00bs.
Here’s a brief outline of the video’s contents:
- Custom lists
- Make a model
- Make a layout
- Make an adapter
- Construct it
- Override getView
- Inflate from XML
In class
With your partner, code up a little image gallery viewer, following these guidelines:
- Grab this image collection. Hook up your device or startup your emulator. Go to the DDMS perspective in Eclipse. Under File Explorer, drag the unzipped image collection onto the SD card, perhaps in the Downloads directory.
- Create a custom adapter to show an image and its caption. The caption is derived from an image’s file name. Strip off the date prefix and the extension, and replace any underscores with spaces.
- Use a GridView to tile the photos. From a developer’s perspective, GridView is almost identical to a ListView. The primary difference is there’s nothing like ListActivity. You’ll have to make your Activity subclass with a layout containing a GridView.
- If you have time, when a user click’s on an item in the GridView, fire up an intent to view the image. Use Intent.setDataAndType to pass in the URI/path (try Uri.fromFile) to the image and the MIME type (“image/jpeg”).
- Programmatically listing the files is done slickly with the File class. Create a File for your image directory. Call listFiles to get an array of Files that are the directories contents.
- To read from the SD card, you’ll need to request permission.