Java Reference
In-Depth Information
Finally, if your CustomItem has the concept of state—that is, if it contains a value—and
the value changes, you must call notifyStateChanged to let any listener on the CustomItem
know that its state has changed.
Implementing a Custom Item
Let's wrap this up with a concrete example. Say the weather application obtains specific
weather conditions—sunny, cloudy, rainy, snowy, and so forth—from a remote server,
and you want to present that information in a graphic format on the Form that shows the
current weather. You could do this one of two ways. You could simply select a particular
image based on the conditions report, or you could encapsulate that functionality in a
CustomItem responsible for drawing the image associated with particular weather condi-
tions. In practice, which you choose may not make that much difference, but for the
purposes of this discussion, let's assume the encapsulation in a CustomItem is better,
because it provides clear encapsulation and responsibility for the data presentation in a
single class. Listing 5-5 shows some of the code necessary to implement the WeatherItem ,
a class that implements the functionality inherited from CustomItem .
Listing 5-5. The WeatherItem, a CustomItem Subclass for Displaying Weather Conditions
package example.wxitem;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class WeatherItem
extends CustomItem {
private String title;
private Display display;
private int width, height;
private boolean hasFocus = false;
private int conditions;
// States (not mutatable by caller!)
public final int SUNNY = 1;
public final int PARTLY_CLOUDY = 2;
public final int CLOUDY = 3;
public final int SHOWERS = 4;
public final int RAIN = 5;
public final int FLURRIES = 6;
public final int SNOW = 7;
public final int SLEET = 8;
 
Search WWH ::




Custom Search