1

I got a simple script:

    int phases = 6;
    final int max = 8;
    final TextView[] a = new TextView[(max * phases)];
    final Button[] b = new Button[phases]; // creates the buttons to display
                                            // the single phases

    for (int x = 0; x < phases; x++) {
        b[x] = new Button(this);
        b[x].setText("yourbutton");
        // linL.addView(b[x]);
        b[x].setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (a[(3)].getVisibility() == 0) {
                    for (int i = 0; i < max; i++)
                        a[i].setVisibility(View.GONE);
                } else {
                    for (int i = 0; i < max; i++)
                        a[i].setVisibility(View.VISIBLE);
                }
            };
        });
    }

This checks basically if a textview is visible and if it's not then it makes it visible (plus the other way round).

My problem is now that I don't want to switch the same text views on again and again, I want to change the views depending on the x of the current loop of the button creation.

However, when i try to include this x, it says that it has to be final.

So how do i get parameters into that on click listener script? (I tried to add them, however it said then that I have to program the whole listener again...that's why I'm asking if there's a smarter way to do it)

Cheers, Christoph

sdabet
  • 18,360
  • 11
  • 89
  • 158
user1497119
  • 443
  • 6
  • 19

2 Answers2

0

If I undertstand correctly, you want to pass parameters to your OnClickListener. I would suggest implementing your own OnClickListener interface - similar to rekaszeru's answer here

Community
  • 1
  • 1
rior
  • 168
  • 2
  • 9
0

You have 2 options:

  1. Before the setOnClickListener line you can declare on another variable as final and you can assign the value of x into it. Then you will have final variable that holds the value of x that it can be used inside the function.
  2. You can implement your own class that implements OnClickListener and you can add a constructor that get the value of x.

Some general notes: I don't know if this is the way you write code or if you masked it when you wrote it here. (Masked, means that you change the variable names that it will be harder to understand what you are doing)

So, if you masked it, please don't, it make it much harder to answer you. If this is the way you write code, I really encourage you to read some articles about coding standards and there importance. Your code isn't indent properly and the names of your variables have no meaning. After you finish with that project, try to read your code again a month later, you will see how hard it is for you to understand what you wrote

nheimann1
  • 2,348
  • 2
  • 21
  • 33
  • The final variable worked fine for me thanks alot, well the code is not masked, instead its just a trail code for me to see how it works and it will be adjusted as soon as i get the input i need to make it final. For testing its easier for me to use letters like a and b, because i am not good in typing thus this eliminates distraction that i get when i have to scroll up and down again and again to check the spelling of variables – user1497119 Nov 14 '12 at 13:08
  • I'm glad that it worked for you. About spelling mistakes, you should use a good IDE with intellisense (Eclipse is a good solution for developing Android applications). If you do use eclipse but you don't utilize the intellisense here is a good place to read about it http://stackoverflow.com/q/2943131/1020530. Everything would be much easier for you if you will adopt some coding standards. – nheimann1 Nov 16 '12 at 10:43