Learning J2ME and Floggy
I’m looking to make an app for my phone that allows me to record my mood each day and plot the data on a graph.
I don’t know Java very well, so I am learning it on the way. I am trying hard not to be impatient; I am tempted to cram as much information into my skull as possible and, in doing so, cause a great deal of frustration.
The first hurdle that I needed to overcome was learning how to save information within J2ME. The standard way of saving anything is to use the Record Management Store API, which, I have read, is exceedingly difficult to use for anything beyond the simplest examples. To this end, others have released their own libraries to make saving data less of a pain.
The solution which I have settled upon is Floggy. In their own words, Floggy is “a free object persistence framework for J2ME/MIDP applications”. Sounds great. Despite their claim that Floggy makes record storage much simpler, I still found using it confusing, though it could have been worse with RMS alone.
I searched Google for a simple example midlet to demonstrate the use of Floggy. (The official examples, a ‘barbecue calculator’ and hospital app, are too complicated for me to grasp.) This site has a short, simple example that saves two items: a name and an age variable. However, it didn’t show me how to load back the data! There isn’t much point to storing it when you can’t get it back.
I modified the example and put in a ‘load file’ command. It works! I’m ecstatic. Here it is:
ExampleFloggy.java
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import net.sourceforge.floggy.persistence.*;
public class ExampleFloggy extends MIDlet implements CommandListener {
private TextField tfield;
private Display display;
private Form form;
private TextField tfieldN;
private Command savecmd;
private Command loadcmd;
private Command exitcmd;
public ExampleFloggy() {
tfield = new TextField("name:", "", 15, TextField.ANY);
tfieldN = new TextField("age:", "0", 15, TextField.NUMERIC);
savecmd = new Command("save", "save file", Command.SCREEN, 0);
loadcmd = new Command("load", "load file", Command.ITEM, 0);
exitcmd = new Command("exit","exit",Command.EXIT,0);
form = new Form("Floggy example.");
form.append(tfield);
form.append(tfieldN);
form.addCommand(savecmd);
form.addCommand(loadcmd);
form.addCommand(exitcmd);
form.setCommandListener(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
notifyDestroyed();
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(form);
}
public void commandAction(Command cmd, Displayable arg1) {
if (cmd == savecmd) {
Date dateOut = new Date(tfield.getString(), Integer.parseInt(tfieldN.getString()));
int id;
try {
id = PersistableManager.getInstance().save(dateOut);
Date dateIn = new Date();
PersistableManager.getInstance().load(dateIn, id);
form.append(dateIn.toString());
} catch (FloggyException e) {
e.printStackTrace();
}
} else if (cmd==exitcmd){
try {
destroyApp(true);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
} else if (cmd==loadcmd) {
try {
ObjectSet set = PersistableManager.getInstance().find(Date.class, null, null);
Date dateIn = new Date();
set.get(0, dateIn);
tfield.setString(dateIn.getName());
tfieldN.setString(Integer.toString(dateIn.getAge()));
} catch (FloggyException e) {
e.printStackTrace();
}
}
}
}
Date.java (save in the same folder as ExampleFloggy.java)
import net.sourceforge.floggy.persistence.Persistable;
public class Date implements Persistable {
private String name;
private int age;
private transient int num;
public Date() {
}
public Date(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "";
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
}
Filed under: Uncategorized | Closed
Tags: j2me