1

I'm trying to set an OnClickListener to some elements in my Android app, these elements are dynamically defined and are not given ids, but they are put in a LinearLayouts which exists in an List of LinearLayouts, so I set the OnClickListener as follows:

    List<LinearLayout> inner_ver = setElemets(1);

       for (LinearLayout l: inner_ver){
                l.getChildAt(0).setOnClickListener(new OnClickListener() { // here's the syntax error
                @Override
                public void onClick(View v) {
                    l.getChildAt(1).setBackgroundResource(R.drawable.home_curtopen);
                }
            });
        }

but I got the syntax error mentioned in the title with l and I can't just declare it as final cause then it assigns the changes only to the last element in the List.

twlkyao
  • 14,302
  • 7
  • 27
  • 44
MRefaat
  • 515
  • 2
  • 8
  • 22

3 Answers3

4

Split into a separate method:

    for (LinearLayout l: inner_ver){
        assignListener(l);
    }

public void assignListener(final LinearLayout l) {
    l.getChildAt(0).setOnClickListener(new View.OnClickListener() { 
        @Override
        public void onClick(View v) {
            l.getChildAt(1).setBackgroundResource(R.drawable.home_curtopen);
        }
    });
}
Greg Ennis
  • 14,917
  • 2
  • 69
  • 74
3

You could also create your own OnClickListener class:

class MyOnClickListener implements OnClickListener{
    private LinearLayout layout;

    public MyOnClickListener(LinearLayout layout){
        this.layout = layout;
    }

    @Override
    public void onClick(View v) {
        layout.getChildAt(1).setBackgroundResource(R.drawable.home_curtopen);
    }

}

Then change your loop to:

for (LinearLayout l: inner_ver){
    l.getChildAt(0).setOnClickListener(new MyOnClickListener(l));
}
FD_
  • 12,947
  • 4
  • 35
  • 62
1

You could add final to your for loop

List<LinearLayout> inner_ver = setElemets(1);

   for (final LinearLayout l: inner_ver){
            l.getChildAt(0).setOnClickListener(new OnClickListener() { // here's the syntax error
            @Override
            public void onClick(View v) {
                l.getChildAt(1).setBackgroundResource(R.drawable.home_curtopen);
            }
        });
    }

Why for(final Type name : Iterable) works is explained in

How does "final int i" work inside of a Java for loop?

Community
  • 1
  • 1
zapl
  • 63,179
  • 10
  • 123
  • 154
  • He said this still only assigned click to the final loop iteration, but given he didn't like my answer either, I think yours is correct and he has a logic error elsewhere in his code. – Greg Ennis Jan 12 '14 at 14:37
  • @GregEnnis most likely. `l.getChildAt(0)` could simply be the same thing all the time. – zapl Jan 12 '14 at 14:40
  • you point is right too ,but i just voted it up and choose the "oldest" right answer as an accepted answer – MRefaat Jan 14 '14 at 09:27
  • 1
    @MRefaat That's fine. The one you accepted is also a bit cleaner in style since it increases legibility. – zapl Jan 14 '14 at 11:30