📄 news.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.io.*;
import java.util.*;
import java.io.*;
import java.lang.*;
/* Declaration of the class News */
public class News extends MIDlet implements CommandListener
{
/* Create an object of class Dataparser */
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 categories ,
news and news details */
private List category, news, details;
/* Declare and create vector type variable for storing news */
private Vector vnews = 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 exitcommand = null;
private Command previouscommand = null;
private Command backcommand = null;
private Command viewcommand = null;
private Command startcommand = null;
/* Declaring variables for Hashtable and initializing with null */
public Hashtable htable = null;
/* Declaring object of Class MainClass */
private MainClass lastscreen;
/* Declaring constructor of Class News */
public News(Display displaynews,MainClass obj)
{
lastscreen = obj;
/* Get the current reference of any object which is present at run time for displaying
on the cellpone screen */
displaylist = displaynews;
/* Create an object of List type to store the list of Categories */
category = new List("Category",List.IMPLICIT);
/* Registering the List object category for event Handling */
category.setCommandListener(this);
/* Creates an object for Ok command button defined as SCREEN and given the priority 1 */
okcommand = new Command("OK",Command.SCREEN,1);
/* Creates an object for Cancel command button defined as SCREEN and given the priority 2 */
cancelcommand = new Command("Cancel",Command.SCREEN,2);
/* Creates an object for Back command button defined as BACK and given the priority 1 */
backcommand = new Command("Back",Command.BACK,1);
/* Creates an object for View command button defined as SCREEN and given the priority 2 */
viewcommand = new Command("View",Command.SCREEN,2);
/* Creates an object for Previous command button defined as BACK and given the priority 1 */
previouscommand = new Command("Previous",Command.BACK,1);
/* Creates an object for Start command button defined as SCREEN and given the priority 2 */
startcommand = new Command("Start",Command.SCREEN,2);
}
/* Declarartion of the method startApp */
/* Application Starts from this method */
public void startApp()
{
/* Store the path of xml file in url object object as string */
String url = new String("http://192.192.168.100/midlet/template/midlet_news.xml");
/* Create 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_category methods for parsing the XML file and inserting
the records in the vector news */
getd.read_category();
/* Calling the returnNews method and returning the records in vector vnews */
vnews = getd.returnNews();
/* Creates an object that implements the Enumeration interface generating a series of elements */
Enumeration e = vnews.elements();
/* The loop execute till any elements are present in Enumeration e object */
while (e.hasMoreElements())
{
/* append all vector values in List category */
category.append( (String)e.nextElement() , null);
}
/* Displays values of List category on cellphone screen */
displaylist.setCurrent(category);
/* To attach the ok command button with List category */
category.addCommand(okcommand);
/* To attach the cancel command button with List category */
category.addCommand(cancelcommand);
}
/* Declaring method for eventhandling routines */
public void commandAction(Command c, Displayable d)
{
int index = 0 ;
/* if condition is associated with the Ok command button when the user clicks it */
if(c == okcommand)
{
/* Creates an object for hashtable htable */
htable = new Hashtable();
/* get the postion which is selected by the user in the List category */
index = category.getSelectedIndex();
/* Retrieving the List value into String according to the index number */
String st = category.getString(index);
/* Storing the path of xml file in url object as string */
String url = new String("http://192.192.168.100/midlet/template/midlet_news_det.xml?ctg="+st );
/* 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 Hashtable */
getd.read_news();
/* Calling the returnHash_id method and returning the records in hashtable */
htable = getd.returnHash_id();
/* Create an object news of List type to display the News of the category selected */
news = new List("News of " + category.getString(index), List.IMPLICIT) ;
/* Registering the List object news for event Handling */
news.setCommandListener(this);
/* 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() ;)
{
/* appending hashtable values in List object news */
news.append((String)e.nextElement(), null);
}
/* displays List object news on cellphone screen */
displaylist.setCurrent(news);
/* To attach the Back Command Button with List object news */
news.addCommand(backcommand);
/* To attach the View Command Button with List object news */
news.addCommand(viewcommand);
}
/* if condition is associated with the Cancel command button when the user clicks it */
if(c == cancelcommand)
{
destroyApp(true);
notifyDestroyed();
/* The getDisplayObject method takes displayobject as parameter and displays the first
screen of the application */
lastscreen.getDisplayObject(displaylist);
}
/* if condition is associated with the Back command button when the user clicks it */
if(c == backcommand )
{
/* displays List object category on cellphone screen */
displaylist.setCurrent(category);
/* To attach the Ok Command Button with List object category */
category.addCommand(okcommand);
/* To attach the Cancel Command Button with List object category */
category.addCommand(cancelcommand);
}
/* if condition is associated with the Start Command Button when the user clicks it */
if(c == startcommand )
{
/* displays List object category on cellphone screen */
displaylist.setCurrent(category);
/* To attach the Ok Command Button with List object category */
category.addCommand(okcommand);
/* To attach the Cancel Command Button with List object category */
category.addCommand(cancelcommand);
}
/* if condition is associated with the View Command Button when the user clicks it */
if(c == viewcommand)
{
/* Create an object of List type to store the details of the News */
details = new List("Details",List.IMPLICIT);
/* declare an String object */
String st = "";
/* get the postion (index number) which is selected by the user in the List object news */
index = news.getSelectedIndex();
/* Retrieving the List value according to the index number */
st = news.getString(index);
/* Retreiving the hashtable value according to the index number */
String hash_id = (String)htable.get(news.getString(index));
/* Storing the path of xml file in String object url */
String url = new String("http://192.192.168.100/midlet/template/midlet_news_description.xml?heading_id="+hash_id);
/* Calling the method sourceurl and sending the path of the xml file */
getd.sourceurl(url);
/* Calling the returnDetails method for parsing XML file and
returning the record into a String */
String st_details = getd.returnDetails();
/* Adding the String to List Object details */
details.append((String)st_details , null);
/* Registering the List object details for event Handling */
details.setCommandListener(this);
/* displays List Object details on cellphone screen */
displaylist.setCurrent(details);
/* To attach the Previous Command Button with List Object details */
details.addCommand(previouscommand);
/* To attach the Start Command Button with List Object details */
details.addCommand(startcommand);
}
/* if condition is associated with previous command button when the user clicks it */
if(c == previouscommand)
{
/* Registering the List object news for event Handling */
news.setCommandListener(this);
/* displays List Object news on cellphone screen */
displaylist.setCurrent(news);
/* To attach the Back Command Button with List Object news */
news.addCommand(backcommand);
/* To attach the Add Command Button with List Object news */
news.addCommand(viewcommand);
}
}
public void destroyApp(boolean b)
{}
public void pauseApp()
{}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -