Common Listener for a bunch of radio groups

I have about 60 radio groups that I need to register a listener for. Currently this is how I do it:

    RadioGroup rg1 = (RadioGroup) getView().findViewById(R.id.rdoGrp1);
    rg1.setOnCheckedChangeListener(this);
    rg1 = (RadioGroup) getView().findViewById(R.id.rdoGrp2);
    rg1.setOnCheckedChangeListener(this);
    rg1 = (RadioGroup) getView().findViewById(R.id.rdoGrp3);
    rg1.setOnCheckedChangeListener(this);

and so on for the 60 radio groups. Is there a way to set all radio groups on the fragment to register to a common listener in a single statement or two without having to type out this much code? Thanks

Answers


If all the RadioGroup elements share the same parent, you could iterate using ViewGroup.getChildAt and ViewGroup.getChildCount. Something like this:

ViewGroup parent = (ViewGroup) findViewById(R.id.ParentViewId);
for (int i = 0; i < parent.getChildCount(); i++) {
    View currentView = parent.getChildAt(i);
    if (currentView instanceof RadioGroup) {
        ((RadioGroup) currentView).setOnCheckedChangeListener(this);
    }
}

Need Your Help

E2E testing remote AngularJS application

testing angularjs cross-domain karma-runner end-to-end

I'm trying to realize E2E testing of existing AngularJS application with Karma Test Runner. My application is located on host1.internal and I'm on host2.internal. So in my karma config file I have:

Javascript functions not working when integrating two codes together

javascript jquery

these javascript functions are working perfectly separate, however when i try to write them in the same page they become inactive. All of the id and class names are correct. I have been fiddleing w...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.