Android: Saving views that have been added programmatically -
as app runs, textviews, buttons , tablerows added programmatically through user inputs. how can save these views have been added, they'll there when user reopens app?
an example of added button:
public void submitpublicquestion(view view) { alertdialog.builder question = new alertdialog.builder(this); question.settitle("submit question"); question.setmessage("enter question: "); // set edittext view user input final edittext input = new edittext(this); final view queue = this.findviewbyid(r.layout.activity_main_class); final button newbtn = new button(this); newbtn.setid((int) (math.random() * 0xffffffff)); question.setview(input); question.setpositivebutton("submit", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { string value = input.gettext().tostring(); newbtn.settext(value); newbtn.setminwidth(550); newbtn.settextsize(24); newbtn.setvisibility(view.visible); linearlayout ll = (linearlayout)findviewbyid(r.id.csquestionslayout); layoutparams lp = new layoutparams(layoutparams.match_parent, layoutparams.wrap_content); newbtn.setlayoutparams(lp); ll.addview(newbtn); } }); question.setnegativebutton("cancel", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { } }); question.show(); }
you can't save actual view
objects on long periods, they're not serializable or parcelable in meaningful way.
what instead save off directions recreating views. each time create view based on user's input, record type of view created, , parameters associated (text, color, position, etc). when app opened, check current state of app against directions list, , recreate views necessary.
Comments
Post a Comment