problem with java 2 me app, cant access the commands -
problem fixed, if interested missing. birthform.setcommandlistener(this);
in getbirthform() function missing, same getloginform()
missing loginform.setcommandlistener(this);. here fixed codes
package badisapp; import java.io.ioexception; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import java.util.vector; public class badismidlet extends midlet implements commandlistener { //=========== fields ======================== private loginform loginform; // login form private notifybirth birthform; // birth notification form private vector birthtype; private vector gender; //========= properties ====================== /** * create login form * @return login form */ public loginform getloginform() { if (loginform == null) { loginform = new loginform("sign in", this); loginform.setcommandlistener(this); } return loginform; } /** * create birth notification form * @return birthform */ public notifybirth getbirthform() { if (birthform == null) { birthform = new notifybirth("birth notification", this.birthtype, this.gender, this);
birthform.setcommandlistener(this); } return birthform; }
//======= constructor ===============// public badismidlet() { gender = new vector(); gender = getgender(); birthtype = new vector(); birthtype = getbirthtype(); loginform = getloginform(); birthform = getbirthform(); } //========== methods ================// /** * create dispaly object */ public display getdisplay() { return display.getdisplay(this); } /** * populate gender option * @return gender vector */ public vector getgender() { vector vector = new vector(); vector.addelement("== choose =="); vector.addelement("male"); vector.addelement("female"); return vector; } public vector getbirthtype() { vector v = new vector(); v.addelement("== choose =="); v.addelement("still born"); v.addelement("live birth"); return v; } /** * switches current screen new screen. * @return void */ public void switchdisplay(alert alert, displayable newdisplayable) { display display = getdisplay(); if (alert == null) { display.setcurrent(newdisplayable); } else { display.setcurrent(alert, newdisplayable); } } /** * start app switching login form */ public void startbadisapp() { switchdisplay(null, birthform); } /** * safely exit midlet */ public void exitbadisapp() { destroyapp(true); notifydestroyed(); } public void commandaction(command command, displayable displayable) { //======== commands login form ============== if (displayable == loginform) { if (command == loginform.getcmdlogin()) { // switchdisplay(null, birthform); } else if (command == loginform.getcmdexit()) { //run exitbadisapp() this.exitbadisapp(); } } else if(displayable == birthform){ if(command == birthform.getcmdsave()){ system.out.print("nvnbvn"); // try { // birthform.insertdata(); // } catch (ioexception ex) { // ex.printstacktrace(); // } } } } public void startapp() { startbadisapp(); } public void pauseapp() { } public void destroyapp(boolean unconditional) { } }
and notifybirth class
package badisapp; import java.io.*; import javax.microedition.lcdui.*; import java.util.vector; import javax.microedition.io.*; public class notifybirth extends form { //=============fields======= private command cmdexit; private command cmdsave; private textfield childfname; private textfield childmname; private textfield childlname; private datefield childdob; private choicegroup birthtype; // hai au amekufa private choicegroup childgender; private textfield bwt; // born weight private textfield placeofbirth; // hospital name private textfield motherfname; private textfield mothermname; private textfield motherlname; private textfield residence; //============ properties ======== public command getcmdexit() { if (cmdexit == null) { cmdexit = new command("exit", command.exit, 2); } return cmdexit; } public command getcmdsave() { if (cmdsave == null) { cmdsave = new command("save", command.ok, 2); } return cmdsave; } public textfield getchildfirstname() { if (childfname == null) { childfname = new textfield("first name", "", 30, textfield.any); } return childfname; } public textfield getchildmiddlename() { if (childmname == null) { childmname = new textfield("middle name", "", 30, textfield.any); } return childmname; } public textfield getchildlastname() { if (childlname == null) { childlname = new textfield("last name", "", 30, textfield.any); } return childlname; } public datefield getchilddob() { if (childdob == null) { childdob = new datefield("date of birth", datefield.date); } return childdob; } public choicegroup getbirthtype() { if (birthtype == null) { birthtype = new choicegroup("birth type", choice.popup); } return birthtype; } public choicegroup initbirthtype(vector btype) { if (birthtype != null) { //load birthtype option (int = 0; < btype.size(); i++) { birthtype.append((string) btype.elementat(i), null); } } return birthtype; } public choicegroup initgender(vector gender) { if (childgender != null) { //load gender option (int = 0; < gender.size(); i++) { childgender.append((string) gender.elementat(i), null); } } return childgender; } public choicegroup getchildgender() { if (childgender == null) { childgender = new choicegroup("gender", choice.popup); } return childgender; } public textfield getbornwithweight() { if (bwt == null) { bwt = new textfield("born weight (in grams)", "", 10, textfield.decimal); } return bwt; } public textfield getplaceofbirth() { if (placeofbirth == null) { placeofbirth = new textfield("place of birth", "", 30, textfield.any); } return placeofbirth; } public textfield getmotherfirstname() { if (motherfname == null) { motherfname = new textfield("mother first name", "", 20, textfield.any); } return motherfname; } public textfield getmothermiddlename() { if (mothermname == null) { mothermname = new textfield("mother middle name", "", 20, textfield.any); } return mothermname; } public textfield getmotherlastname() { if (motherlname == null) { motherlname = new textfield("mother last name", "", 20, textfield.any); } return motherlname; } public textfield getresidence() { if (residence == null) { residence = new textfield("residence", "", 30, textfield.any); } return residence; } public void insertdata() throws ioexception { httpconnection httpconn = null; string phpurl = "http://127.0.0.1/badis/index.php/report/trial/"; inputstream inputstream = null; outputstream outputstream = null; stringbuffer buffer = null; try { // open http connection object httpconn = (httpconnection) connector.open(phpurl); // setup http request post httpconn.setrequestmethod(httpconnection.post); httpconn.setrequestproperty("user-agent", "profile/midp-1.0 confirguration/cldc-1.0"); httpconn.setrequestproperty("accept_language", "en-us"); //content-type must pass parameters in post request httpconn.setrequestproperty("content-type", "application/x-www-form-urlencoded"); outputstream = httpconn.opendataoutputstream(); string child = "child=" + childfname; outputstream.write(child.getbytes()); buffer = new stringbuffer(); inputstream = httpconn.opendatainputstream(); int chr; while ((chr = inputstream.read()) != -1) { buffer.append((char) chr); } // web server returns birthday in mm/dd/yy format. system.out.println(child + "'s first name " + buffer.tostring()); } finally{ if(httpconn != null){ httpconn.close(); } if(outputstream != null){ outputstream.close(); } if(inputstream != null){ inputstream.close(); } } } //============= construct ======== public notifybirth(string title, vector btype, vector gender, badismidlet parent) { super(title); //create commands cmdexit = getcmdexit(); cmdsave = getcmdsave(); childfname = getchildfirstname(); childmname = getchildmiddlename(); childlname = getchildlastname(); childdob = getchilddob(); childgender = getchildgender(); childgender = initgender(gender); bwt = getbornwithweight(); placeofbirth = getplaceofbirth(); birthtype = getbirthtype(); birthtype = initbirthtype(btype); residence = getresidence(); motherfname = getmotherfirstname(); mothermname = getmothermiddlename(); motherlname = getmotherlastname(); //add form this.addcommand(cmdexit); this.addcommand(cmdsave); this.append(childfname); this.append(childmname); this.append(childlname); this.append(childdob); this.append(birthtype); this.append(childgender); this.append(bwt); this.append(motherfname); this.append(mothermname); this.append(motherlname); this.append(placeofbirth); this.append(residence); } }
happy coding..
cheers
Comments
Post a Comment