i creating app, in create edittexts dynamically id number.
i want pass information edittexts, tried create edittext array these , use .gettext().tostring() save them in string array, want pass next activity.
it seems won't create "editarray[]" in second code part correctly.
thanks in advance.
here's code (enternames.java) - creation of edittexts -> succesful
protected void numberofedittext() { view vertlayout = (linearlayout) findviewbyid(r.id.vertlayout); string spinvalue = getintent().getextras().getstring("spinvalue"); int intspinvalue = integer.valueof(spinvalue); edittextcount = intspinvalue; edittext[] edittextarray = new edittext[edittextcount]; (int = 0; < edittextcount; i++) { string name = "name " + (i+1); edittextarray[i] = new edittext(this); edittextarray[i].setid(i+1000); edittextarray[i].settext(name); edittextarray[i].settextsize(20); edittextarray[i].setfilters( new inputfilter[] { new inputfilter.lengthfilter(15) } ); edittextarray[i].setselectallonfocus(true); edittextarray[i].setsingleline(true); edittextarray[i].setlayoutparams(new layoutparams(layoutparams.wrap_content,layoutparams.wrap_content)); ((linearlayout) vertlayout).addview(edittextarray[i]); } }
second code (enternames.java) - passing data next activity -> failure. testing think problem for-loops (the editarray returns null)
public void go(view view) { setcontentview(r.layout.activity_enter_names); string namearray [] = new string [edittextcount]; edittext editarray [] = new edittext [edittextcount]; intent intent = new intent(this, randomgeneration.class); (int = 0; < edittextcount; i++) { editarray[i] = (edittext) findviewbyid(i+1000); } (int = 0; < edittextcount; i++) { namearray[i] = editarray[i].gettext().tostring(); } bundle extras = new bundle(); extras.putint("numberofnames", edittextcount); extras.putstringarray("namearray", namearray); intent.putextras(extras); startactivity(intent); }
according activity.findviewbyid()
documentation, method search views in xml, not in views added in java.
the simple way want:
class enternames extends activity { private int edittextcount; private edittext[] edittextarray; protected void numberofedittext() { linearlayout vertlayout = (linearlayout) findviewbyid(r.id.vertlayout); edittextcount = integer.valueof(getintent().getextras().getstring("spinvalue")); edittextarray = new edittext[edittextcount]; layoutparams params = new layoutparams(layoutparams.wrap_content,layoutparams.wrap_content); (int = 0; < edittextcount; i++) { string name = "name " + (i+1); edittextarray[i] = new edittext(this); edittextarray[i].settext(name); edittextarray[i].settextsize(20); edittextarray[i].setfilters( new inputfilter[] { new inputfilter.lengthfilter(15) } ); edittextarray[i].setselectallonfocus(true); edittextarray[i].setsingleline(true); edittextarray[i].setlayoutparams(params); vertlayout.addview(edittextarray[i]); } } […] public void go(view view) { // delete views added linearlayout, don't use it! //setcontentview(r.layout.activity_enter_names); string namearray[] = new string[edittextcount]; (int = 0; < edittextcount; i++) { namearray[i] = editarray[i].gettext().tostring(); } bundle extras = new bundle(); extras.putint("numberofnames", edittextcount); extras.putstringarray("namearray", namearray); intent intent = new intent(this, randomgeneration.class); intent.putextras(extras); startactivity(intent); } }
Comments
Post a Comment