My J2ME Programming Contact (1)--list

Source: Internet
Author: User
Programming
Import javax.microedition.lcdui.*;
Import javax.microedition.midlet.*;
public class Listtest extends MIDlet implements commandlistener{

Private List alist;
Private Command Exitcommand;
Private Command Okcommand;
Private Display Adisplay;
Private Alert Analert;


Public Listtest () {
Analert=new Alert ("Selected", "You have Choose a Fruit", null,alerttype.info);

Alist=new List ("fruit", choice.multiple);
Exitcommand=new Command ("Exit", command.exit,1);
Okcommand=new Command ("OK", command.ok,1);

Alist.append ("Apple", null);
Alist.append ("Orange", null);
Alist.append ("banana", null);

Alist.addcommand (Exitcommand);
Alist.addcommand (Okcommand);

Alist.setcommandlistener (this);
}

public void startApp () {

Adisplay=display.getdisplay (this);
Adisplay.setcurrent (alist);

}

public void Pauseapp () {

}

public void Destroyapp (Boolean unconditional) {

}

public void Commandaction (Command c,displayable D) {
Use the I get the Selected list element
int i =alist.getselectedindex ();

if (C==exitcommand) {
Destroyapp (FALSE);
Notifydestroyed ();

}

else {
Display different information when diffenent element
Have been selected
/*if (i==0) {
Analert.setstring ("You have choosed apple!");
Adisplay.setcurrent (analert,alist);
}

if (i==1) {
Analert.setstring ("You have choosed orange!");
Adisplay.setcurrent (analert,alist);
}

if (i==2) {
Analert.setstring ("You have choosed banana!");
Adisplay.setcurrent (analert,alist);
}*/
String s= "";
for (int i=0;i<alist.size (); i++) {

Boolean selected=alist.isselected (i);
if (selected&&i==0) {
s+= "Apple";
}

if (selected&&i==1) {
s+= "Orange";
}

if (selected&&i==2) {
s+= "Banana";
}
}

Analert.setstring ("You have Selected" +s);
Analert.settimeout (Alert.forever);
Adisplay.setcurrent (analert,alist);


}



}



}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.