How to pass argument in a different way in Android/Java? -


i'm building android app in implement supertype method of activity called oncheckedchanged(compoundbutton buttonview, boolean ischecked) follows:

@override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {     if (ischecked){         linearlayout view = (linearlayout) findviewbyid(r.id.some_view);         animation anim = expand(view, true);         view.startanimation(anim);     }     else {         linearlayout view = (linearlayout) findviewbyid(r.id.some_view);         animation anim = expand(view, false);         view.startanimation(anim);     } } 

and method set listen switch in oncreate method this:

myswitch = (switch) findviewbyid(r.id.my_switch); myswitch.setoncheckedchangelistener(this); 

the thing want implement method not some_view, couple other views. since don't want copy/paste method couple times , change some_view need way of passing view. since i'm overriding method however, cannot add argument method. since set listener method can not set id global variable before method invoked.

so question is: how can pass id method don't need copy/paste method reuse several views?

implement own oncheckedchangelistener , 1 of 2 options fitting needs:

  1. use multiple instances of listener. 1 each pair of <switcher, linearlayout>.
  2. use 1 instance of listener holding array of linearlayouts animate of them when switching 1 switcher instance.

this code, both options.

public class myoncheckedchangelistener implements compoundbutton.oncheckedchangelistener {      private final view[] mviews;      public myoncheckedchangelistener(view... views) {         mviews = views     }      @override     public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {         (view v : mviews) {             linearlayout layout = (linearlayout) v;             if (ischecked) {                 animation anim = expand(layout, true);                 layout.startanimation(anim);             } else {                 animation anim = expand(layout, false);                 layout.startanimation(anim);             }         }     } }  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     // inflate layout      // option 1     myswitch = (switch) findviewbyid(r.id.my_switch);     myswitch.setoncheckedchangelistener(             new myoncheckedchangelistener(findviewbyid(r.id.some_view)));     myotherswitch = (switch) findviewbyid(r.id.my_other_switch);     myotherswitch.setoncheckedchangelistener(             new myoncheckedchangelistener(findviewbyid(r.id.some_other_view)));      // option 2     myswitch = (switch) findviewbyid(r.id.my_switch);     myswitch.setoncheckedchangelistener(             new myoncheckedchangelistener(new view[]{                     findviewbyid(r.id.some_view),                     findviewbyid(r.id.some_other_view)             })); } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -