Subscribe:

Ads 468x60px

Pages

Friday, February 25, 2011

How I jumped into MAD with J2ME....

Well..., After finishing the first semester of my university life I got a break to study about technological stuffs on my interest. so I'm studying about MAD these days... :D Hey..., I'm talking about Mobile Application Delveloping, however that's a kind of mad. As I got to know There's so many ways to develop mobile apps but I used J2ME with Sun Java(TM) Wireless Toolkit because java enabled moble devices are widely available in this time. First I just wanted know how to build small app for my little mobile phone (Sony Ericsson C702) as new fellow to this subject. Finally I got some knowlege by reviewing some web sites, pdf's, sample codes, etc. That's how I jumped into water. Then I wanted make a app which can be useful for someone. When I was thinking about it one of my friend gave me a hint for it. He has developed a little application containing info's (index numbers, registration numbers, etc) about students in our Batch which runs on computers. I saw a disadvantage of  that app, we cannot access those info's without a computer. That reduce the mobility of the application and the usage of it. So I thought that can be dropped out if I can implement that on mobile devices like our mobile phones. Then I did it within two days as my first app. And it was a practical solution for that problem. I'll attach my app to this and you also can try it... 

Code:


import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class Example extends MIDlet implements CommandListener, ItemStateListener {

    private static final Command CMD_EXIT = new Command("Exit", Command.EXIT, 5);
    private Display display;
    private boolean firsttime;
    private Form mainForm;
    private ChoiceGroup fullName, name, index, regNo, email;
    private Ticker ticker;
    
    /** I used this arrays to store data. In this case I have used five arrays but I think it can be done by using a 2D array also. So it's up to you   **/
  
String[] fullNameArray = {"example_1","example_2","...etc"};
    String[] nameArray = {"example_1","example_2","...etc"};
    String[] indexArray = {"example_1","example_2","...etc"};
    String[] regNoArray = {"example_1","example_2","...etc"};
    String[] emailArray = {"example_1","example_2","...etc"};

    public Example() {
        firsttime = true;
    }

    protected void startApp() {
        if (firsttime) {
            display = Display.getDisplay(this);

            mainForm = new Form("Example");
            mainForm.setTicker(getTicker());

            ChoiceGroup[] groups = {getFullNameChoiceGroup(), getNameChoiceGroup(), getRegChoiceGroup(), getIndexChoiceGroup(), getEmailChoiceGroup()};

            for (int iter = 0; iter < groups.length; iter++) {
                mainForm.append(groups[iter]);
            }

            mainForm.addCommand(CMD_EXIT);
            mainForm.setItemStateListener(this);
            mainForm.setCommandListener(this);
            firsttime = false;
        }

        display.setCurrent(mainForm);
    }

    public void commandAction(Command c, Displayable d) {
        if (d == mainForm) {
            if (c == CMD_EXIT) {
                destroyApp(false);
                notifyDestroyed();
            }
        }
    }

    protected void destroyApp(boolean unconditional) {
    }

    protected void pauseApp() {
    }

    public ChoiceGroup getFullNameChoiceGroup() {
        if (fullName == null) {
            fullName = new ChoiceGroup("Full name", ChoiceGroup.POPUP, fullNameArray, null);
        }
        return fullName;
    }

    public ChoiceGroup getNameChoiceGroup() {
        if (name == null) {
            name = new ChoiceGroup("First name", ChoiceGroup.POPUP, nameArray, null);
        }
        return name;
    }

    public ChoiceGroup getIndexChoiceGroup() {
        if (index == null) {
            index = new ChoiceGroup("Index", ChoiceGroup.POPUP, indexArray, null);
        }
        return index;
    }

    public ChoiceGroup getRegChoiceGroup() {
        if (regNo == null) {
            regNo = new ChoiceGroup("Reg No", ChoiceGroup.POPUP, regNoArray, null);
        }
        return regNo;
    }

    public ChoiceGroup getEmailChoiceGroup() {
        if (email == null) {

            email = new ChoiceGroup("E-mail", ChoiceGroup.POPUP, emailArray, null);

        }
        return email;
    }

    public void refresh(int in) {
        fullName.setSelectedIndex(in, true);
        name.setSelectedIndex(in, true);
        index.setSelectedIndex(in, true);
        regNo.setSelectedIndex(in, true);
        email.setSelectedIndex(in, true);
    }

    /** This itemStateChanged() method is a important point of this code. Because it has dropped the use of another UI command **/
    public void itemStateChanged(Item item) {
        int in = 0;
        if (item == fullName) {
            in = fullName.getSelectedIndex();
        } else if (item == name) {
            in = name.getSelectedIndex();
        } else if (item == index) {
            in = index.getSelectedIndex();
        } else if (item == regNo) {
            in = regNo.getSelectedIndex();
        } else if (item == email) {
            in = email.getSelectedIndex();
        }
        refresh(in);
    }

    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {


        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }

    }

    public Ticker getTicker() {
        if (ticker == null) {
            ticker = new Ticker("Developed by: Supun Viraj Rathnayaka  ----  E_mail: rathnaviraj@gmail.com  ----");
        }
        return ticker;
    }
}
           

No comments:

Post a Comment