Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to OnClickListener?

How can i pass parameter to an OnClickListener() ?

Got my Listener:

   OnClickListener myListener = new OnClickListener()    {       @Override      public void onClick(View v)      {          //I want to reach params here       }    }; 

I got 12 buttons and i dont want to write 12 listeners for them, it would be great to just pass a string to them and they can do completly different things.

like image 221
Adam Varhegyi Avatar asked May 16 '12 08:05

Adam Varhegyi


People also ask

Can we pass parameter in onClick function?

It's very easy to Pass parameter to JavaScript function using the onClick() function. If sending a string value then use double” ” or single ” quote in the function.

How do you pass parameters on onClick react?

To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .

How pass parameter from JavaScript to HTML?

Use the onclick attribute in a button tag with the function name and pass value in this function. With this method, you can also take input from users and pass parameters in the JavaScript function from HTML.


1 Answers

Use your own custom OnClickListener

public class MyLovelyOnClickListener implements OnClickListener    {       int myLovelyVariable;      public MyLovelyOnClickListener(int myLovelyVariable) {           this.myLovelyVariable = myLovelyVariable;      }       @Override      public void onClick(View v)      {          //read your lovely variable      }    }; 
like image 95
Sherif elKhatib Avatar answered Sep 28 '22 18:09

Sherif elKhatib