android - Reset all Views in fragment -
so in fragmenta.java, class consists of different edittext
s , checkbox
es users press , input.
my question is, have reset button, how can reset entire fragment view? (e.g. edittext
set empty string, or value 0, when created).
p.s. of course can set edittext/checkboxes 1 one programically, there quite lot of them , other views, know if there way reset of them.
let's break down steps:
1. getting references
how done depends on have. if fields created in-code, it's easy: store references in list<commonbasetype>
.
if loaded xml layout, there multiple options. if want views of type(s) reset, can iterate through view hierarchy getting reference holding viewgroup
(the layout) , iterate on children getchildcount()
, getchildat(int)
. then, check type of child. if it's viewgroup
, check it's children. if it's edittext
or checkbox
, add them list.
if need more control , don't want all views reset, can tag ones want something. can tag view in xml using android:tag
-attribute , find them after inflation using view.findviewwithtag(object)
-method.
2. resetting
now have references, can reset them iterating on collection made in step 1 , handle them depending on type. pseudo code that:
list<view> form_elements = findviewstoreset(); (view element : form_elements){ if (element instanceof edittext){ ((edittext) element).settext(""); } else if (element instanceof checkbox){ ((checkbox) element).setchecked(false); } // , forth... }
something reset fields in form default-value, depending on type.
3. resetting original values
if want reset views original values, should "index" when initial values set (which might directly after inflation, if set values via xml).
to this, run through list step 1 , make mapping id value @ point:
list<view> form_elements = findviewstoreset(); map<integer, object> default_values = new hashmap<>(form_elements.size()); (view element : form_elements){ if (element.getid() == view.no_id){ // have nothing identify view by... continue; } // store default values away: if (element instanceof edittext){ default_values.put( element.getid(), ((edittext) element).gettext() ); } else if (element instanceof checkbox){ default_values.put( element.getid(), ((checkbox) element).ischecked() ); } // , forth... }
later when want reset form-elements, can iterate list again , default values map. cast them depending on type of field (edittext
-> string
, checkbox
-> boolean
, etz) , set values.
bonus: nasty radiogroup
resetting radiogroup archived calling clearcheck()
on it, has nasty side-effect of triggering associated oncheckedchangelistener
(which might not want, depending on you're doing in listener).
the simplest way around un-register listener before calling clearcheck()
, re-registering afterwards. can archived overriding radiogroup.clearcheck()
:
/** * when {@link #clearcheck()} called, registered (if any) {@link android.widget.radiogroup.oncheckedchangelistener} <b>not</b> called. * @author lukas knuth * @version 1.0 */ public class customradiogroup extends radiogroup { private oncheckedchangelistener checked_change_listener; public customradiogroup(context context) { super(context); } public customradiogroup(context context, attributeset attrs) { super(context, attrs); } @override public void setoncheckedchangelistener(oncheckedchangelistener listener) { // need store ourselves, since there no getter-method listener -.- this.checked_change_listener = listener; super.setoncheckedchangelistener(listener); } @override public void clearcheck() { // 1. unregister listener: super.setoncheckedchangelistener(null); // 2. clear super.clearcheck(); // 3. restore listener before: super.setoncheckedchangelistener(this.checked_change_listener); } }
Comments
Post a Comment