Java Reference
In-Depth Information
currentEntry = (WeightEntry) database.elementAt(index - 1);
SwitchView(VIEW_EDIT_ENTRY);
}
}
}
In the code fragment above, notice that WeightEntry is a simple
private utility class used to encapsulate a data record - for simplicity, it
doesn't have accessors or mutators, just public fields (i.e. it is basically a
Java version of a C struct ).
The EditEntryView class is similar to the main view as they're
both high-level screens, however in this case a number of controls are
used to allow the user to edit an existing record or to create a new
one. Notice how this class shows that you don't have to implement the
ComponentListener interface. For a simple input screen like this, we
can cheat a bit and use the softkey events to handle data validation and
updating the database:
private class EditWeightView extends Panel implements SoftKeyListener {
private ListBox cboMonths, cboDays;
private TextBox txtWeight;
private int entryId;
public EditWeightView(WeightEntry weightEntry) {
entryId = weightEntry.Id;
// populate date controls
int lastDay = Utilities.getLastDayOfMonth(weightEntry.Month,
Calendar.getInstance().get(Calendar.YEAR));
cboDays = new ListBox(ListBox.CHOICE);
for(int i = 1; i <= lastDay; i++) {
cboDays.append(" " + i);
} cboMonths = new ListBox(ListBox.CHOICE);
for(int i = 0; i < 12; i++) {
cboMonths.append(Utilities.monthName(i));
} cboDays.select(weightEntry.Day - 1);
cboMonths.select(weightEntry.Month);
txtWeight = new
TextBox(Float.toString(weightEntry.Weight),5,1,TextBox.NUMBER);
setTitle("Weight Data");
add(new Label("Day"));
add(cboDays);
add(new Label("Month"));
add(cboMonths);
add(new Label("Weight (kg)"));
add(txtWeight);
Search WWH ::




Custom Search