Passing the text of a Button from one Activity to another Activity

0

First of all forgive my ignorance, I've been learning to program for Android for 2 days and I'm missing something.

As the title says try to pass the text of a button from one activity to another

The code of the first activity:

private Button btnBorrachos;
private Button btnAnimales;

private String sendBundle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnBorrachos = (Button) findViewById(R.id.buttonBorrachos);
    btnAnimales = (Button) findViewById(R.id.buttonAnimales);


    btnBorrachos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            sendBundle = btnBorrachos.getText().toString();
            Intent intent = new Intent(MainActivity.this, PruebaActivity.class);
            intent.putExtra("txtbundle", sendBundle);
            startActivity(intent);
        }
    });

    btnAnimales.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendBundle = btnAnimales.getText().toString();
            Intent intent = new Intent(MainActivity.this, AnimalesActivity.class);
            intent.putExtra("txtbundle", sendBundle);
            startActivity(intent);
        }
    });
}

What I'm trying to do is that depending on the button that is given, I will capture the text of that button in the sendBundle variable and with Intent send it to the second activity.

Code of the second activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prueba);

    Bundle bundle = getIntent().getExtras();
    switch (bundle.getString("txtbundle")) {
        case "Borrachos":
            Toast.makeText(PruebaActivity.this, "Ha llegado 'Borrachos'", Toast.LENGTH_LONG);
            break;
        case "Animales":
            Toast.makeText(PruebaActivity.this, "Ha llegado 'Animales'", Toast.LENGTH_LONG);
            break;
        default:
            Toast.makeText(PruebaActivity.this, "Ha llegado DESCONOCIDO", Toast.LENGTH_LONG);
    }

}

Supposedly what I should do is that depending on the text that comes from each button I show a Toast or another. But it does not show any in any case.

the comparison seems to be fine, these are the texts of the buttons:

android:text="Borrachos"
android:text="Animales"
    
asked by Rodrypaladin 10.11.2018 в 12:59
source

1 answer

0

As they said in the comments you need the .show in the Toast, but I also use the View.OnClickListener directly in the Activity for the use you want to give, and then I would catch the botton that was, you also take away the need for global variables.

public class MainActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button myButton;
        myButton = (Button) findViewById(R.id.buttonBorrachos);
        myButton.setOnClickListener(this);
        myButton = (Button) findViewById(R.id.buttonAnimales);
        myButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String sendBundle = ((Button)v).getText().toString();       
        Intent intent;
        switch(sendBundle){
            case "Animales":
                intent = new Intent(MainActivity.this, AnimalesActivity.class);
                break;
            case "Borrachos":
                intent = new Intent(MainActivity.this, PruebaActivity.class);
                break;
        }
        intent.putExtra("txtbundle", sendBundle);
        startActivity(intent);
    }
}
    
answered by 15.11.2018 в 15:19