android - How to Sum values from EditText on ListView? -


i have lists of color variations of product in listview . each list color variation has edittext. want try make validation process orders when button clicked.

this code:

btnorder.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {              int count = listview.getadapter().getcount();             string[] listdata = new string[count];             int[] listdata2 = new int[count];             int sum = 0;             try {                (int = 0; < count; i++) {                 view quantity=listview.getchildat(i);                 if (quantity.findviewbyid(r.id.quantityorder) != null){                     edittext quantityorder = (edittext) quantity.findviewbyid(r.id.quantityorder);                     listdata[i] = quantityorder.gettext().tostring();                     listdata2[i] = integer.parseint(quantityorder.gettext().tostring());  // set edittext int                     sum += listdata2[i];                         jsonobject.put("params_"+i,listdata[i]);   // put params volley request                  }                 }                 if (sum < 1) {toast.maketext(getapplicationcontext(),                         "sorry, need fill order quantity", toast.length_short)  // validation input if edittext empty                         .show();} else {                        log.d(tag, jsonobject.tostring()); }             } catch (jsonexception e) {                 e.printstacktrace();               }          }     }); 

my app force close. here error code

09-25 23:01:05.679  32623-32623/id.nijushop.ikutan e/androidruntime﹕ fatal exception: main java.lang.numberformatexception: invalid int: ""         @ java.lang.integer.invalidint(integer.java:138)         @ java.lang.integer.parseint(integer.java:359)         @ java.lang.integer.parseint(integer.java:332)         @ id.nijushop.ikutan.productdetail$1.onclick(productdetail.java:150)         @ android.view.view.performclick(view.java:4084)         @ android.view.view$performclick.run(view.java:16966)         @ android.os.handler.handlecallback(handler.java:615)         @ android.os.handler.dispatchmessage(handler.java:92)         @ android.os.looper.loop(looper.java:137)         @ android.app.activitythread.main(activitythread.java:4745)         @ java.lang.reflect.method.invokenative(native method)         @ java.lang.reflect.method.invoke(method.java:511)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553)         @ dalvik.system.nativestart.main(native method) 

please, 1 fix code...i think need

quantity.findviewbyid(r.id.quantityorder)// need set interger 

the problem in line:

listdata2[i] = integer.parseint(quantityorder.gettext().tostring());  // set edittext int 

the integer.parseint(string string) method return int or throw numberformatexception if passed string not valid integer string. empty string - "" doesn't make valid integer, if edittext empty, problem arises.

you need shield execution of parseint try - catch block catching numberformatexception , act accordingly - abort method or else, cannot continue arithmetic if haven't provided valid number.

also, thing is, within edittext element in xml file, include inputtype property, example:

<edittext             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:inputtype="number|numbersigned" /> 

this inputtype property cause system use automatic inputfilter , provideonly numeric soft keyboard, user cannot enter invalid number (in case, signed integer). still not account empty input, you'll need either catch numberformatexception or check whether string edittext not empty. (!string.isempty())


Comments