📄 weather.java
字号:
/* Importing the basic packages required by various classes
during execution of the program */
import java.util.Hashtable;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.io.*;
import java.util.*;
import java.io.*;
import java.lang.*;
/* Declaration of the class Weather. */
public class Weather extends MIDlet implements CommandListener
{
/* Declare an object for GetData class */
DataParser getd;
/* Declaring variable for requesting that object to be displayed on the device */
private Display displaylist;
/* Declaring List type variables for storing and displaying list of cities and details of weather */
private List states, details;
/* Declare and create vector type variables for storing list of cities and list of labels */
private Vector vstate = new Vector();
private Vector vlist = new Vector();
/* Declaring variables for Command Buttons and initializing with null */
private Command cancelcommand = null;
private Command okcommand = null;
private Command goback = null;
private Command exit = null;
/* Declaring Hashtable object and initialising it with null */
private Hashtable htable = null;
/* Creating object of RecordStore type and initialising it with null */
RecordStore recordstore = null;
/* Declaring object of Class MainClass */
private MainClass lastscreen;
/* Declaring constructor of Class Weather */
public Weather(Display displayweather, MainClass obj)
{
/* initializing Mainclass object lastscreen with the argument obj */
lastscreen = obj;
/* Getting the current reference of any object which is present at run time
for displaying it on the cellpone screen */
displaylist = displayweather;
/* Create an object of List type to store the list of Cities */
states = new List("States",List.IMPLICIT);
/* Registering the List object states for event Handling */
states.setCommandListener(this);
/* Creates an object for hashtable htable */
htable = new Hashtable();
/* Creates an object for exit command defined as screen and give the priority 2 */
exit = new Command("Exit",Command.EXIT,2);
/* Creates an object for Back command defined as Command and given the priority 2 */
goback = new Command("Back",Command.BACK,1);
/* Creates an object for Ok command defined as screen and given the priority 1 */
okcommand = new Command("OK",Command.SCREEN,1);
/* Creates an object for Cancel command defined as Screen and given the priority 2 */
cancelcommand = new Command("Cancel",Command.SCREEN,2);
/* Inserts label into vector vlist */
vlist.insertElementAt("State",0);
vlist.insertElementAt("Date",1);
vlist.insertElementAt("Sunrise",2);
vlist.insertElementAt("Sunset",3);
vlist.insertElementAt("Moonrise",4);
vlist.insertElementAt("Moonset",5);
vlist.insertElementAt("Day Humidity",6);
vlist.insertElementAt("Night Humidity",7);
vlist.insertElementAt("Day Wind Direction",8);
vlist.insertElementAt("Night Wind Direction",9);
vlist.insertElementAt("Day Wind Speed",10);
vlist.insertElementAt("Night Wind Speed",11);
vlist.insertElementAt("High Temp",12);
vlist.insertElementAt("Low Temp",13);
vlist.insertElementAt("Rain Fall",14);
vlist.insertElementAt("Forecast",15);
}
/* Declarartion of the method startApp */
/* Application Starts from this method */
public void startApp()
{
/* Store the path of xml file in url object as string */
String url = new String("http://192.192.168.100/midlet/template/midlet_weather.xml");
/* Creating an object for DataParser class */
getd = new DataParser();
/* Calling the method sourceurl and sending the path of the xml file */
getd.sourceurl(url);
/* Calling the read_record method for parsing XML file and
storing the records into the Database and Hashtable */
getd.read_record();
/* Calling the returnState method and returning the records in hashtable */
htable = getd.returnState();
/* Creates an object that implements the Enumeration interface generating a series of elements
and the loop will execute till any element is present in Enumeration e object */
for (Enumeration e = htable.keys(); e.hasMoreElements() ;)
{
/* Adding values to List Object states, present in the hashtable */
states.append( (String)e.nextElement() , null);
}
/* display List states on cellphone screen */
displaylist.setCurrent(states);
/* To attach the ok command button with List states */
states.addCommand(okcommand);
/* To attach the cancel command button with List states */
states.addCommand(cancelcommand);
}
/* Declaring method for eventhandling routines */
public void commandAction(Command c, Displayable d)
{
/* if condition is associated with ok command button when the user clicks it */
if(c == okcommand)
{
String st = "";
/* get the postion which is selected by the user in the List states */
int index = states.getSelectedIndex();
/* Retrieving the hashtable value according to the index number */
String hash_id = (String)htable.get(states.getString(index));
/* Create an object details of List type to display the Weather Details */
details = new List("Weather of " + states.getString(index) , List.IMPLICIT) ;
/* Registering the List object details for event Handling */
details.setCommandListener(this);
/* displays List details on cellphone screen */
displaylist.setCurrent(details);
try
{
/* Opens the Recordstore addresses for retrieving the records */
recordstore = RecordStore.openRecordStore("addresses", true);
/* Retreiving the record from the recordstore and storing it as String */
st = new String(recordstore.getRecord(Integer.parseInt(hash_id)));
/* Closes the Recordstore after retrieving the records */
recordstore.closeRecordStore();
}
catch(RecordStoreException rse)
{
rse.printStackTrace();
}
/* Declaring integer variables */
int st_index = 0, end_index = 0, ctr = 0, pos ;
/* Storing the string length retrieved from the database */
int len = st.length();
/* Declaring String type variable for storing the substring */
String sub_st = "";
/* Declaring the character type array, creating space for array and
defining the size equivalent to string length */
char[] c_arr = new char[len];
/* Inserting String into character type array. */
c_arr = st.toCharArray();
/* For Loop for retreiving Weather record from the string and displaying it
on the cellphone screen */
for (pos = 0; pos < len ; pos++)
{
if(c_arr[pos] == '?')
{
st_index = end_index ;
end_index = pos ;
ctr = ctr + 1;
if (ctr > 1)
{
sub_st = st.substring(st_index + 1,end_index);
details.append((String)vlist.elementAt(ctr - 2) + " " + sub_st, null);
}
}
}
/* To attach the goback and exit command button with List details */
details.addCommand(goback);
details.addCommand(exit);
}
/* if condition is associated with exit command button when the user clicks it */
if(c == exit)
{
/* Destroys the weather application */
destroyApp( true );
/* Confirms application destroyed */
notifyDestroyed();
/* The getDisplayObject method takes displayobject as parameter and displays the first screen of the application */
lastscreen.getDisplayObject(displaylist);
}
/* if condition is associated with cancel command button when the user clicks */
if(c == cancelcommand)
{
/* Destroys the weather application */
destroyApp(true);
/* Confirms application destroyed */
notifyDestroyed();
/* The getDisplayObject method takes displayobject as parameter and displays the first screen ot the application */
lastscreen.getDisplayObject(displaylist);
}
/* if condition is associated with goback command button when the user clicks */
if(c == goback)
{
/* displays List states on cellphone screen */
displaylist.setCurrent(states);
/* To attach the ok and cancel command button with List states */
states.addCommand(okcommand);
states.addCommand(cancelcommand);
}
}
/* Method called when the application is destroyed */
public void destroyApp(boolean b)
{
/* Deletes the records from the database by calling the deleterecords method */
getd.deleterecords();
}
/* This method is called when the midlet is paused */
public void pauseApp()
{}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -