java - How can I create RequiredFieldValidattion in Android? -
here code. looks bit complicated it's working fine. want know how can create when click btncreateproduct should validate required edit text field , add code?
package com.prinsapps.whatson; import java.util.calendar; import android.app.activity; import android.app.datepickerdialog; import android.app.dialog; import android.os.bundle; import android.view.view; import android.app.progressdialog; import android.content.intent; import android.os.asynctask; import android.util.log; import android.widget.button; import android.view.view.onclicklistener; import android.widget.datepicker; import android.widget.edittext; import android.widget.imagebutton; import java.util.arraylist; import java.util.list; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonexception; import org.json.jsonobject; import com.prinsapps.whatson.addevent.createnewproduct; public class addevent extends activity implements onclicklistener { private imagebutton ib; private calendar cal; private int day; private int month; private int year; private edittext et; //progress dialog private progressdialog pdialog; jsonparser jsonparser = new jsonparser(); edittext inputname; edittext inputdesc; edittext inputcountry; edittext inputdate; edittext inputlink; edittext inputorg; // url create new product private static string url_create_product = "http://-----------"; // json node names private static final string tag_success = "success"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.add_event); //edit text inputname = (edittext) findviewbyid(r.id.inputname); inputdesc = (edittext) findviewbyid(r.id.inputdesc); inputcountry = (edittext) findviewbyid(r.id.inputcountry); inputdate = (edittext) findviewbyid(r.id.inputdate); inputlink = (edittext) findviewbyid(r.id.inputlink); inputorg = (edittext) findviewbyid(r.id.inputorg); // mdatebutton = (button) findviewbyid(r.id.date_button); ib = (imagebutton) findviewbyid(r.id.imagebutton1); cal = calendar.getinstance(); day = cal.get(calendar.day_of_month); month = cal.get(calendar.month); year = cal.get(calendar.year); et = (edittext) findviewbyid(r.id.inputdate); ib.setonclicklistener(this); // create button button btncreateproduct = (button) findviewbyid(r.id.btncreateproduct); // button click event btncreateproduct.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // creating new product in background thread new createnewproduct().execute(); } }); } @override public void onclick(view v) { showdialog(0); } @override @deprecated protected dialog oncreatedialog(int id) { return new datepickerdialog(this, datepickerlistener, year, month, day); } private datepickerdialog.ondatesetlistener datepickerlistener = new datepickerdialog.ondatesetlistener() { public void ondateset(datepicker view, int selectedyear, int selectedmonth, int selectedday) { et.settext(selectedyear+"/"+(selectedmonth+1)+"/"+selectedday); } }; /** * background async task create new product * */ class createnewproduct extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(addevent.this); pdialog.setmessage("creating product.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } /** * creating product * * */ protected string doinbackground(string... args) { string name = inputname.gettext().tostring(); string description = inputdesc.gettext().tostring(); string country = inputcountry.gettext().tostring(); string date = inputdate.gettext().tostring(); string link = inputlink.gettext().tostring(); string org = inputorg.gettext().tostring(); // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("name", name)); params.add(new basicnamevaluepair("description", description)); params.add(new basicnamevaluepair("country", country)); params.add(new basicnamevaluepair("date", date)); params.add(new basicnamevaluepair("link", link)); params.add(new basicnamevaluepair("org", org)); // getting json object // note create product url accepts post method jsonobject json = jsonparser.makehttprequest(url_create_product, "post", params); // check log cat fro response log.d("create response", json.tostring()); // check success tag try { int success = json.getint(tag_success); if (success == 1) { // created product intent = new intent(getapplicationcontext(), allproductsactivity.class); startactivity(i); // closing screen finish(); } else { // failed create product } } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog once done pdialog.dismiss(); } } }
as i'm assuming, don't want empty textfield empty if creating product, i'd suggest check wether strings
of edittexts
null or empty.
this this:
if(inputname.gettext().equals("") || inputname.gettext().equals(null)){ //maybe show message, given field ist required filled out }
i suggest add onclick()
method, or write method check wether edittext empty , return boolean value. this:
public boolean notempty(edittext et){ if(et.gettext().equals("") || et.gettext().equals(null)){ return false; } return true; }
and check every edittext
et before create new product.
Comments
Post a Comment