java - Android: code refactoring advice -
i creating todo list kind of app. app 7 screens set target , complete that. screenshot here . while coding realized there lots of redundancy in code, like:
1> 7 day of week need create 7 fragments 7 fragments need perform same operations. can w/o 7 fragments?
2> there 15 checkboxes , 15 textview all-i need reference, perform onclick operations separately on each 1 , settext , gettext on each user try modify them.
here 1 of textview modify code:
ptext2 = (textview) view.findviewbyid(r.id.p_textview2); ptext2.setonlongclicklistener(new view.onlongclicklistener() { @override public boolean onlongclick(view view) { alertdialog.builder alertdialog = new alertdialog.builder(getactivity()); alertdialog.settitle("target"); alertdialog.setmessage("set target"); final edittext input = new edittext(getactivity()); alertdialog.setview(input); alertdialog.setpositivebutton("set", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { string targetinp = input.gettext().tostring(); ptext2.settext(targetinp); p2 = ptext2.gettext().tostring(); addeventstodb(); } }); alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { } }); alertdialog.show(); return true; } });
similarly there 15 more textview , checkbox need 7 fragments. here full code of 1 of fragment
since kind of apps first app of users learning android development should done improve code?
edit: multiple buttons onclick listener's creating inner class , using switch button specific operations.
switch(view.getid()){ case r.id.id1: //do break; ..... }
from this answer. need figure out 7 fragments (can done 1 fragment?)
what naming implementation of view.onlongclicklistener
class like:
class ondayclicklistener extends view.onlongclicklistener { @override public boolean onlongclick(final view view) { // here use view instead of ptext2 } }
then use
ptext2.setonlongclicklistener(new ondayclicklistener()); // repeats every textbox
Comments
Post a Comment