Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

hudi ilfeld
hudi ilfeld
1,221 Points

using an onClick method inside a loop

I have written an anonymous inner class(onClickListener) including the onClick method inside a FOR loop as following:

//the code.

if(checkFirst.contains("Kobi") && checkLast.contains("Rubin")){

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);
            }

}); }

//end of code.

The problam is after i have lauched the app when i click the button in the app, it does not create a second activity. by the way, when i chave removed the hole OnClickListener class and recoded it outside the loop the app was running succesfuly. How do i fix the problam???

hudi ilfeld
hudi ilfeld
1,221 Points

Its a IF loop not a FOR loop. but the idea is the same!!

2 Answers

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Hudi,

What are you trying to accomplish with the if-statement?

As written, when your code runs... the onClickListener will only be set if the if-statement is true. If it is false, then the click listener will not be set and your button will not listen to any click events.

hudi ilfeld
hudi ilfeld
1,221 Points

I want the button to launch the second activity only if the user has inserted the specific names Wich I wrote in the IF loop...

Jon Kussmann
PLUS
Jon Kussmann
Courses Plus Student 7,254 Points

Hi Hudi,

Given your most recent comment, this is how I would structure your code.

btn.setOnClickListener(new View.OnClickListener() {
      @Override
       public void onClick(View v) {
            if(checkFirst.contains("Kobi") && checkLast.contains("Rubin")){
                  Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                  startActivity(intent);
          }
       }
}); 

In this case your button will always "listen" for clicks. When the button is clicked, your program will check the fields for those two values. If the if-statement holds true, it will start the new activity. If not, nothing will happen.

I hope this helps.

hudi ilfeld
hudi ilfeld
1,221 Points

hi jon. To tell you the truth, initially i have try'd doing so as i noticed the problam.. but again android studio would not let me do so... only now i have noticed that i couldn't access the variables(checkFirst,checkLast) since they where outside the anonymus inner class so i have removed them into the onClick method and now the app is running well!! anyway thanks for the help.