Thursday, April 28, 2011

J2ME List Image

List Image MIDlet Example

This example illustrates how to create list with image symbol. In this
example we are trying to create list using of List class. The List class define
in two type of constructors, that is:




  • List(String title, int listType)
  • List(String title, int listType, String[] stringElements, Image[]
    imageElements)
     

The List class has following Methods:

  • append(String stringPart, Image imagePart):- This method is the
    integer type, which is used to append an element to the choice. 
  • delete(int elementNum):- This is the void type which deletes the
    element. 
  • getImage(int elementNum):- This is the Image type which gets the
    image.
  • getSelectedFlags(boolean[] selectedArray_return):- This is integer
    type method which has the state of a Choice and returns the state of all
    elements in the boolean array. 
  • getSelectedIndex():- This is the integer type which returns the
    index number of an element in the Choice that is selected. 
  • getString(int elementNum):- This method is used to get the String
    of the element.
  • insert(int elementNum, String stringPart, Image imagePart):- This
    is void type it inserts an element into the Choice just prior to the element
    specified. 
  • isSelected(int elementNum):- It gets the boolean value indicating
    whether element is selected or not. 
  • set(int elementNum, String stringPart, Image imagePart):- It sets
    the element to the specified element, replacing the previous contents of the
    element. 
  • setSelectedFlags(boolean[] selectedArray)
  • setSelectedIndex(int elementNum, boolean selected)
  • size()

The SELECT_COMMAND is used to recognized whether user select from list
or not.


 

 

 





Source Code of ListImage.java 

 




import java.io.IOException;

import javax.microedition.lcdui.*;

import javax.microedition.midlet.*;



public class ListImage extends MIDlet implements CommandListener{


  private Display display;

  private List list;

  private Command exit, next;

  private Image car, airplane, hotel, mobile, cartoon;


  String[] stringElements = {"Aero plane""Car""Hotel""Mobile"};

  


  public ListImage(){

    try{

      airplane = Image.createImage("/airplane.png");

      car = Image.createImage("/car1.png");


      hotel = Image.createImage("/hotel1.png");

      mobile = Image.createImage("/mobile_ico.png");

      cartoon = Image.createImage("/cartoon.png");


    }catch(Exception e){

      System.err.println(e.getMessage());

    }

  }




  public void startApp() {

    display = Display.getDisplay(this);

    Image[] imageElements = {airplane, car, hotel, mobile};


    list = new List("List + Image", List.IMPLICIT, 

stringElements, imageElements
);





    next = new Command("Select", Command.SCREEN, 0);

    exit = new Command("Exit", Command.EXIT, 0);


    list.addCommand(next);

    list.addCommand(exit);

    list.setCommandListener(this);


    display.setCurrent(list);

  }



  public void pauseApp() {}



  public void destroyApp(boolean unconditional){


    notifyDestroyed();

  }



  public void commandAction(Command c, Displayable s){


    int index = list.getSelectedIndex();

    if (c == next || c == List.SELECT_COMMAND) {


      Alert alert = new Alert("Selected""You have selected: 

+ list.getString(index






        + 
".", cartoon, AlertType.INFO);

      display.setCurrent(alert, list);


    else if(c == exit){

      destroyApp(true);

    }


  }

}





 

Download Source Code

No comments:

Post a Comment

Search This Blog

Total Pageviews