java - Convert Android.Widget.Button to JSON -
i making app have save dynamically created android.widget.button-objects , attributes, id, when app closed , opened again.
these buttons saved in arraylist.
my idea convert button-objects json , save them in sharedpreference.
my problem cant convert buttons json, using following code this, if found on stackoverflow:
(for tryouts using new button-object)
button btn = new button(this); gson gson = new gson(); string json = gson.tojson(btn);
its working string-object or integer-object not button-object.
can me?
if create buttons dynamically means set color, text, ... them. when want save them need know how many buttons had , custom attributes you've set each of them.
so can that: manage 2 lists, 1 buttons , 1 custom attributes. make easier can use custom buttonbuilder manage attributes. each time want new button, create new buttonbuilder, set attributes, generate button , store both builder , button in 2 separated lists. can store list of builders in sharedprefs , regenerate buttons list next time open app.
list<buttonbuilder> mbuilders = new arraylist<>(); list<button> mbuttons = new arraylist<>(); /** * display new button */ public void addbutton(/* list of parameters*/) { buttonbuilder builder = new buttonbuilder() .setbgcolor(mycolor) .settext(mytext); button button = builder.build(context); mbuilders.add(builder); mbuttons.add(button); // ... display button } /** * call method when need regenerate buttons */ public void regeneratebuttonsonstart() { // shared preferences mbuilders = getbuildersfromsharedprefs(); button btn; (buttonbuilder builder : mbuilders) { btn = builder.build(context); mbuttons.add(btn); // ... display button } } /** * custom button builder */ public class buttonbuilder { private int mbgcolor; private string mtext; // ... whatever want public buttonbuilder() { } public buttonbuilder setbgcolor(int bgcolor) { this.mbgcolor = bgcolor; return this; } public buttonbuilder settext(string text) { this.mtext = text; return this; } public button build(context context) { button btn = new button(context); btn.settext(mtext); btn.setbackgroundcolor(mbgcolor); return btn; } }
Comments
Post a Comment