0

In my program, I have 2 spinners: one shows a list of 3 numbers (as strings: one, two, and three), and another shows a list of countries: USA, Canada, Mexico, and Brazil. I also have a button that when tapped, goes to another activity/screen in android studio.

My question is, how should I use the variables returned from a spinner to go to another activity in android studio?

Here are the things I know:

The variables from the spinner 'exist' and can be used; I tried displaying them, and the TextView displays them. Here is the code that initializes the spinners:

//INIT SPINNER 1
Spinner spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.numbers, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);

// INIT SPINNER 2
Spinner countries = findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countries.setAdapter(adapter2);
countries.setOnItemSelectedListener(this);

and here's the code that displays the two variables in one TextView:

TextView display = findViewById(R.id.display_spinner);
String text1 = spinner.getSelectedItem().toString();
String text2 = countries.getSelectedItem().toString();
display.setText(text1+text2);

The button, when used without any conditional statements relating to the spinners' variables, it opens a new activity. This is code for just opening a new activity by default:

//BUTTON INIT
button = (Button) findViewById(R.id.open_india);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        openIndia();
    }
});

Also, when using a simple conditional such as

if(4-3 == 1){
   startActivity(intent);
}

the code works perfectly, and when I click the button on the emulator, I go to another activity/screen.

My question is, how do I use the variables returned from a spinner to determine whether or not to go to another activity/screen? I have the code for it, but for some reason, the button just decides not to go to the next activity.

The variables are set VARIABLE.toString(), so I know that the variable is a string, so it makes sense that it can be compared to another string.

String text2 = countries.getSelectedItem().toString();
if(text2 == "India"){
    startActivity(intent);
} 

Could anyone shed some light on this issue?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

2 Answers2

2

Change the condition to :

if(text2.equals("India")){
    startActivity(intent);
}
Huzaifa Iftikhar
  • 755
  • 1
  • 6
  • 9
0

You do not use spinner.getSelectedItem (). ToString ().

Instead of this

spinner.getSelectedItemPosition ();

spinner.getSelectedItemId ();

Use this.

public static final Int INDIA_CODE = 1;
int itemPos = countries.getSelectedItemPosition();
int itemId = countries.getSelectedItemId();
if(itemId == INDIA_CODE) {
    startActivity(intent);
}
ChaMinGyu
  • 26
  • 5