java - How can you add multiple button listeners all at once? -


scroll down answer. question doesn't matter , code confusing.

is there function allow me fetch id labels can loop button listener easily? have in main "container" xml houses fragments line of code:

public static final map<string, integer> rmap = createmapr(); // map of widget id's in r  private static map<string, integer> createmapr() {     map<string, integer> result = new hashmap<string, integer>();      (field f : r.id.class.getdeclaredfields()) {         string key = f.getname();         integer value = 0;         try {              value = f.getint(f);         }         catch (illegalargumentexception e) {             e.printstacktrace();         }         catch (illegalaccessexception e) {             e.printstacktrace();         }         result.put(key, value);     }     return collections.unmodifiablemap(result); } 

and 1 of fragments pick rmap, , cross labels have specified. i'm doing because have few buttons , specifying huge list of listeners seemed inefficient, got sidetracked on code.

public class bottomfragment extends fragment {   private final string[] labels = {"button_do_1", "button_woah_2", "button_foo_1",                                   "button_hi_2"};  @override public view oncreateview(layoutinflater inflater, viewgroup container,          bundle savedinstancestate) {     view v = inflater.inflate(r.layout.bottom_fragment, container, false);     (string item : labels) {             if (container.rmap.containskey(item)) {             ((button) v.findviewbyid(container.rmap.get(item)))                 .setonclicklistener(this);         }     }     return v; } 

however if there way iterate through list of android:id items bottomfragment() wouldn't have need first block of code , eliminate manual list of id's i've typed in.

here's rough logic sketch (in c#)... single, top down pass on layout, building id-to-view map can looped through afterwards. there know general way this, , have maintained along layout file (both need modified together).

say want in oncreate of activity hosting fragment(s) (it possibly done in fragments onmeasure or onlayout overrides too)

assuming fragment's layout file has following structure


linearlayout     textview     textview      relativelayout        edittext       edittext     textview 

public override oncreate() {    map<int, view> idlabels = new map<int, view>();     view v = fragment.view;     linearlayout ll = (linearlayout) v;    idlabels.add(ll.id, ll)     textview tv1 = (textview) ll.getchildat(0);    idlabels.add(tv1.id, tv1)     textview tv2 = (textview) ll.getchildat(1);    idlabels.add(tv2.id, tv2)     relativelayout rl = (relativelayout) ll.getchildat(2);    idlabels.add(rl.id, rl)      // notice numbers being passed getchildat, traversal of views , viewgroups !     edittext et1 = (edittext) rl.getchildat(0);    idlabels.add(et1.id, et1)     edittext et2 = (edittext) rl.getchildat(1);    idlabels.add(et2.id, et2)     // now, go "up" top-most, linear layout viewgroup , remaining views, if     textview tv3 = (textview) ll.getchildat(3);    idlabels.add(tv3.id, tv3)     ...       foreach(int id in idlabels)    {        if(id == r.id.myviewidlabel) // r file have id's in literal form.        {            view v = idlabels.map(id);            v.setonclicklistener(this);        }      }  } 

this may not best way, should work.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -