CSCI 490E/680K Assignment 4a - Android Programming - Spring 2012
03/09/2012 - first draft; subject to minor revisions
Overview
This application is the first of several versions that implement the idea of a Weather Log app. Each entry in the Weather Log will contain 3 data items (all Strings):
The three fields for each log entry will be displayed as three successive TextViews in a scrolling list of log entries. The ListView of these entries will be populated dynamically by a custom data adapter.
In addition, the main Activity will have a single options menu button, Add, which will cause a second Activity to be displayed in which the user can type in a temperature and some notes (the time/date will be automatically generated) and then can either Save or Cancel (via two Buttons). When Save is pressed, the new entry will display at the end of the list of log entries on the main screen.
xml files
main.xml - will define a ListView root element and its attributes including an id. It will fill the screen. No child elements will be included. (see weather_entry.xml)
weather_entry.xml - will define the fields and layout of a single log entry - the 3 TextViews. Include id's for each. This file will be used to "inflate" each log entry's objects to be filled with one log entry's data.
weather_add.xml - will define the screen for entering data for a new log entry: a label, and an EditText (for temperatures); a label and an EditText (for notes) and two Buttons (Save and Cancel).
The Main Activity
This class will extend Activty. In onCreate(), it will setContent() to main.xml. This object will create and "own" a reference to the ListView in main.xml. It will also create and "own" a reference to a WeatherTrackerAdapter (see below) which will connect the data (defined and "owned" in the adapter class) to the display. This listview will then set the weathertrackeradapter as its adapter.
The Custom WeatherTracker Adapter
This activity will extend BaseAdapter. It will define and hold an ArrayList of WeatherLogEntryData objects (see below). In its constructor, create and add a few weather log entries to the ArrayList so you have something to see right away on the screen. You will need to override several methods in this class to make it functional:
int getCount() - have it return the ArrayList's size.
Object getItem(int index) - have it return the index-th object in the ArrayList
long getItemId(int index) - have it just return its argument, index.
View getView(int index, View view, ViewGroup parent) - this method does the main work of inflating the xml for an entry (weather_entry.xml). Using the View returned by inflate() it will get the references to the individual fields in the View and populate them with corresponding values from the ArrayList data. Recall that this method will be called multiple times, once for each item in the ArrayList.
WeatherLogEntryData class
This simple class should encapsulate a single weather log entry. That is, it should hold the three Strings that constitute an entry. It should have set/get methods for each field. Its constructor should take two String arguments (for temperature and notes) and store them into the object's instance variables. It should use the java.util.Date class to generate the time/date field.