Setting A Onclick even for multiple buttons

Put your problem here if it does not fit any of the other categories.

Setting A Onclick even for multiple buttons

Postby jasonpeinko » Wed Nov 05, 2008 7:35 am

So im getting into android development and I decided to make a tic tac toe app.

I have an array of buttons and what i want to do is loop through the array and assign a button event.
Like this, but the i and j values don't carry over.

Code: Select all
buttons[i][j].setOnClickListener(new Button.OnClickListener()
         {
              public void onClick(View view)
              {
                 setToken(i,j);
              }
         });

Code: Select all
public void  setToken(int i,int j)
     {
        tokens[i][j]='X';
        buttons[i][j].setText(""+tokens[i][j]);
     }
jasonpeinko
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Nov 05, 2008 7:30 am

Top

Postby MrSnowflake » Wed Nov 05, 2008 12:08 pm

You'd do this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. buttons[i][j].setOnClickListener(onClickListener);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

where onClickListener:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Button.OnClickListener onClickListener = new Button.OnClickListener()
  2.  
  3.          {
  4.  
  5.               public void onClick(View view)
  6.  
  7.               {
  8.  
  9.                  // You can use the setToken() here
  10.  
  11.                  // (though it's supposed to be better to use:
  12.  
  13.                  // MyClass.this.setToken(i,j);
  14.  
  15.                  //setToken(i,j);
  16.  
  17.                  // Or put you setToken() stuff in here:
  18.  
  19.                  tokens[i][j]='X';
  20.  
  21.                  buttons[i][j].setText(""+tokens[i][j]);
  22.  
  23.               }
  24.  
  25.          });
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby jasonpeinko » Wed Nov 05, 2008 5:06 pm

It does not work, it still sets the button of the values [2][2] to x no matter what button is pressed because it is the last value stored in i,j.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.        for(i=0; i<2; i++)
  3.  
  4.          {
  5.  
  6.            for(j=0; j<2; j++)
  7.  
  8.            {
  9.  
  10.                 buttons[i][j].setOnClickListener(onClickListener);
  11.  
  12.            }
  13.  
  14.          }
  15.  
  16.      }
  17.  
  18.      Button.OnClickListener onClickListener = (new Button.OnClickListener()
  19.  
  20.      {
  21.  
  22.           public void onClick(View view)
  23.  
  24.           {
  25.  
  26.              tokens[i][j]='X';
  27.  
  28.              buttons[i][j].setText(""+tokens[i][j]);
  29.  
  30.           }
  31.  
  32.      });
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
jasonpeinko
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Nov 05, 2008 7:30 am

Postby chris » Tue Nov 11, 2008 10:34 am

Hi,

there's no way this is going to work because you are assigning the same onClickListener to all your boutons and your onClickListener must be referencing some class attributes i and j (otherwise it won't compile).

You have 2 solution : create 1 listener by button and store the references on the listener :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.   for(i=0; i<2; i++)
  3.  
  4.          {
  5.  
  6.            for(j=0; j<2; j++)
  7.  
  8.            {   Button.OnClickListener onClickListener = (new Button.OnClickListener()
  9.  
  10.      { public i,j;
  11.  
  12.           public void onClick(View view)
  13.  
  14.           {
  15.  
  16.              tokens[i][j]='X';
  17.  
  18.              buttons[i][j].setText(""+tokens[i][j]);
  19.  
  20.           }
  21.  
  22.      });
  23.  
  24. onClickListener.i=i;
  25.  
  26. onClickListener.j=j;
  27.  
  28.                 buttons[i][j].setOnClickListener(onClickListener);
  29.  
  30.            }
  31.  
  32.          }
  33.  
  34.      }
  35.  
  36.    
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


and another one more elegant would be to make your Activity implements onClickListener :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    
  2.  
  3.  
  4.  
  5. public class myActiviy implements Button.OnClickListener
  6.  
  7. ...
  8.  
  9.            for(j=0; j<2; j++)
  10.  
  11.            {
  12.  
  13.                 buttons[i][j].setOnClickListener(this);
  14.  
  15.            }
  16.  
  17.          }
  18.  
  19.      }
  20.  
  21.  
  22.  
  23. ...
  24.  
  25.          public void onClick(View view)
  26.  
  27.           {
  28.  
  29.  for(i=0; i<2; i++)
  30.  
  31.          {
  32.  
  33.            for(j=0; j<2; j++)
  34.  
  35.            {
  36.  
  37.               if (buttons[i][j].getId() == view.getId()){
  38.  
  39.    tokens[i][j]='X';
  40.  
  41.              buttons[i][j].setText(""+tokens[i][j]);
  42.  
  43.            }
  44.  
  45.  
  46.  
  47.            
  48.  
  49.           }
  50.  
  51.      });
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Cheers
chris
Junior Developer
Junior Developer
 
Posts: 18
Joined: Sat Feb 02, 2008 4:39 pm

Postby MrSnowflake » Tue Nov 11, 2008 11:23 am

That's because you assign the value to button[i][j], you have to assign it to the button you pressed.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      Button.OnClickListener onClickListener = (new Button.OnClickListener()
  2.      {
  3.           public void onClick(View view)
  4.           {
  5.              view.setText(""+ tokens.get(view));
  6.           }
  7.      });
  8.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
For the record: This onClickListener is a field and not a local variable!!!
Also, make you tokens[][] a HashSet with the view as key, so you can se my code (just edit it some to fit your app).

chris wrote:and another one more elegant would be to make your Activity implements onClickListener :
...
Cheers
In this case that could be a good idea, but then you can only handle 1 type of onClick action, with my approach you can have as many as the Java limits permit.
Last edited by MrSnowflake on Tue Nov 11, 2008 11:26 am, edited 1 time in total.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 18 guests