java - Sharing an Array between OnClick classes in Android Studio -
i'm writing code takes input user, , user either clicks "enter activity" button or "done" button. i'm taking information within enteranotherbutton onclicklistener, need able transfer information donebutton onclicklistener send activity via intent. here's current code:
public class entercourseactivity extends actionbaractivity { private mainactivity ma; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_enter_course); buttonclick(); } public void buttonclick () { button enteranotherbutton = (button) findviewbyid(r.id.enteranotherbutton); enteranotherbutton.setonclicklistener(new view.onclicklistener() { int = 1; public void onclick(view view) { edittext coursename; coursename = (edittext) findviewbyid(r.id.inputcoursename); string coursenamestring = coursename.gettext().tostring(); string[] courses = new string[i]; courses[i - 1] = coursenamestring; i++; coursename.settext(""); } }); button donebutton = (button) findviewbyid(r.id.donebutton); donebutton.setonclicklistener(new view.onclicklistener(){ public void onclick(view view){ } }); //when user clicks donebutton execute arraylist<string> coursesarraylist = new arraylist<string>(arrays.aslist(courses)); intent = new intent(entercourseactivity.this, mainactivity.class); i.putstringarraylistextra("course", coursesarraylist); startactivity(i); }
as @feantury suggested, create class house information saving. can access same object upon donebutton click.
public void buttonclick () { final sampleobj obj = new sampleobj("test"); button enteranotherbutton = (button) findviewbyid(r.id.enteranotherbutton); enteranotherbutton.setonclicklistener(new view.onclicklistener() { int = 1; public void onclick(view view) { obj.coursename = "test1"; } }); button donebutton = (button) findviewbyid(r.id.donebutton); donebutton.setonclicklistener(new view.onclicklistener(){ public void onclick(view view){ log.d(tag, "result of obj: " + obj.coursename); } }); } class sampleobj { string coursename; public sampleobj(string coursename) { this.coursename = coursename; } }
clicking enteranotherbutton changes obj.coursename "test1", accessible in onclick of donebutton.
Comments
Post a Comment