java - Managing several EditTexts in Android -
possible duplicate:
android: how elegantly set many button ids
everyone.
i'm trying build first android app doing rms (root mean square) error calculation. need several experimental points inputs, done manually in edittexts. have 40 edittexts in 1 layout. problem when comes managing them, have this:
public class insertardata extends activity implements onclicklistener { edittext x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17; edittext y1,y2,y3,y4,y5,y6,y7,y8,y9,y10,y11,y12,y13,y14,y15,y16,y17; float [] datosx; float [] datosy; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.insertardatos); x1=(edittext)findviewbyid(r.id.x1et); x2=(edittext)findviewbyid(r.id.x2et); x3=(edittext)findviewbyid(r.id.x3et); x4=(edittext)findviewbyid(r.id.x4et); x5=(edittext)findviewbyid(r.id.x5et); x6=(edittext)findviewbyid(r.id.x6et); x7=(edittext)findviewbyid(r.id.x7et); x8=(edittext)findviewbyid(r.id.x8et); x9=(edittext)findviewbyid(r.id.x9et); x10=(edittext)findviewbyid(r.id.x10et); x11=(edittext)findviewbyid(r.id.x11et); x12=(edittext)findviewbyid(r.id.x12et); x13=(edittext)findviewbyid(r.id.x13et); x14=(edittext)findviewbyid(r.id.x14et); x15=(edittext)findviewbyid(r.id.x15et); x16=(edittext)findviewbyid(r.id.x17et); x17=(edittext)findviewbyid(r.id.x17et); y1=(edittext)findviewbyid(r.id.y1et); y2=(edittext)findviewbyid(r.id.y2et); y3=(edittext)findviewbyid(r.id.y3et); y4=(edittext)findviewbyid(r.id.y4et); y5=(edittext)findviewbyid(r.id.y5et); y6=(edittext)findviewbyid(r.id.y6et); y7=(edittext)findviewbyid(r.id.y7et); y8=(edittext)findviewbyid(r.id.y8et); y9=(edittext)findviewbyid(r.id.y9et); y10=(edittext)findviewbyid(r.id.y10et); y11=(edittext)findviewbyid(r.id.y11et); y12=(edittext)findviewbyid(r.id.y12et); y13=(edittext)findviewbyid(r.id.y13et); y14=(edittext)findviewbyid(r.id.y14et); y15=(edittext)findviewbyid(r.id.y15et); y16=(edittext)findviewbyid(r.id.y16et); y17=(edittext)findviewbyid(r.id.y17et); button continuar=(button)findviewbyid(r.id.continuarbtn); continuar.setonclicklistener(this); } @override public void onclick(view e) { switch(e.getid()){ case r.id.continuarbtn:{ datosx=new float[17]; datosx[0]=float.parsefloat(x1.gettext().tostring()); datosx[1]=float.parsefloat(x2.gettext().tostring()); datosx[2]=float.parsefloat(x3.gettext().tostring()); datosx[3]=float.parsefloat(x4.gettext().tostring()); datosx[4]=float.parsefloat(x5.gettext().tostring()); datosx[5]=float.parsefloat(x6.gettext().tostring()); datosx[6]=float.parsefloat(x7.gettext().tostring()); datosx[7]=float.parsefloat(x8.gettext().tostring()); datosx[8]=float.parsefloat(x9.gettext().tostring()); datosx[9]=float.parsefloat(x10.gettext().tostring()); datosx[10]=float.parsefloat(x11.gettext().tostring()); datosx[11]=float.parsefloat(x12.gettext().tostring()); datosx[12]=float.parsefloat(x13.gettext().tostring()); datosx[13]=float.parsefloat(x14.gettext().tostring()); datosx[14]=float.parsefloat(x15.gettext().tostring()); datosx[15]=float.parsefloat(x16.gettext().tostring()); datosx[16]=float.parsefloat(x17.gettext().tostring()); datosy[0]=float.parsefloat(y1.gettext().tostring()); datosy[1]=float.parsefloat(y2.gettext().tostring()); datosy[2]=float.parsefloat(y3.gettext().tostring()); datosy[3]=float.parsefloat(y4.gettext().tostring()); datosy[4]=float.parsefloat(y5.gettext().tostring()); datosy[5]=float.parsefloat(y6.gettext().tostring()); datosy[6]=float.parsefloat(y7.gettext().tostring()); datosy[7]=float.parsefloat(y8.gettext().tostring()); datosy[8]=float.parsefloat(y9.gettext().tostring()); datosy[9]=float.parsefloat(y10.gettext().tostring()); datosy[10]=float.parsefloat(y11.gettext().tostring()); datosy[11]=float.parsefloat(y12.gettext().tostring()); datosy[12]=float.parsefloat(y13.gettext().tostring()); datosy[13]=float.parsefloat(y14.gettext().tostring()); datosy[14]=float.parsefloat(y15.gettext().tostring()); datosy[15]=float.parsefloat(y16.gettext().tostring()); datosy[16]=float.parsefloat(y17.gettext().tostring()); } } } }
this "works" (it needs completed passing datosx , datosy activity. but, before, need know whether edittexts empty or not , cannot imagine doing if(edittext.gettext().tostring()!=null) before every parsefloat.
is there better way manage lot of edittexts , know if they're empty or not? i've though of array of edittext , doing like:
edittext editarray[17]; editarray[0]=(edittext)findviewbyid(r.id.edittext0);
is way of doing it?
thank reading , answering in advance. sorry english.
how this?
edittext xe, ye; float x, y; int[] xids = { r.id.x1et, r.id.x2et, /*...etc*/ }; int[] yids = { r.id.y1et, r.id.y2et, /*...etc*/ }; for(int = 0; < xids.length && < yids.length; i++) { xe = (edittext)findviewbyid(xids[i]); ye = (edittext)findviewbyid(yids[i]); try { x = float.parsefloat(xe.gettext().tostring()); y = float.parsefloat(ye.gettext().tostring()); //do whatever need data here } catch (numberformatexception e) { //either there invalid input, or field empty //you can add logic here check them empty fields //or exit loop here } }
Comments
Post a Comment