0

I'm trying one thing: in my Activity I have 30 buttons inside them for everyone there is a number that goes from 1 to 30. Now I want to assign each an Intent that opens the same Activity but passing the corresponding number. How can I do?

public void ApriTavolo(View v) { Bundle extras = new Bundle(); // pass the value of button extras.putString("one", one); // Perform action on click Intent activityChangeIntent = new Intent(MainActivity.this, Interno_tavolo.class); activityChangeIntent.putExtras(extras); startActivity(activityChangeIntent); } 

2 Answers 2

1

If you have just number in button, make sure that all goes the same listener method,

and get the text and send the text as parameter.

public void ApriTavolo(View v) { Bundle extras = new Bundle(); Button b = (Button) v; String value = b.getText().toString(); // pass the value of button extras.putString("value", value); // Perform action on click Intent activityChangeIntent = new Intent(MainActivity.this, Interno_tavolo.class); activityChangeIntent.putExtras(extras); startActivity(activityChangeIntent); } 
Sign up to request clarification or add additional context in comments.

Comments

0

you could dod something like

String pressed = null; switch(v.getId()) { case R.id.firstButton: pressed = "one"; break; // up to the end } Intent activityChangeIntent = new Intent(MainActivity.this, Interno_tavolo.class); extras.putString("one", one); activityChangeIntent.putExtras(extras); startActivity(activityChangeIntent); 

or you could assign a tag to each button (there is the property android:tag); and the you can retrieve it like:

Intent activityChangeIntent = new Intent(MainActivity.this, Interno_tavolo.class); extras.putString("one", (String)v.getTag()); activityChangeIntent.putExtras(extras); startActivity(activityChangeIntent) 

1 Comment

Perfect. I did not know the Tag property

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.