0

I ahve an application with a spinner drop down menu. When I tap Admin from spinner drop down menu it does not go to Admin.class. And same with Teacher, when I tap Teacher from spinner drop down menu it does not go to Student.class.\

My code:

public class Home extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        String[] sp1 = getResources().getStringArray(R.array.Level);
        Spinner spinner = (Spinner)findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sp1);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String s = parent.getItemAtPosition(position).toString();
                if(s=="Admin")
                    startActivity(new Intent(Home.this,Admin.class));
                if(s=="Teacher")
                    startActivity(new Intent(Home.this,Student.class));   
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });    
    }
}
jww
  • 97,681
  • 90
  • 411
  • 885
Zohra Rao
  • 37
  • 1
  • 3
  • 6
  • Possible duplicate of [Difference between Equals/equals and == operator?](http://stackoverflow.com/questions/971954/difference-between-equals-equals-and-operator) and [How do I compare strings in Java?](http://stackoverflow.com/q/513832/608639) – jww Feb 02 '15 at 01:53

2 Answers2

1

Use the firstString.equals(secondString) function to compare strings, not the == operator.

The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with ==, but it's better not to rely on that.

Do it this way

@Override
public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
    String s = parent.getItemAtPosition(position).toString();
    if(s.equals("Admin"))
        startActivity(new Intent(Home.this,Admin.class));
    if(s.equals("Teacher"))
        startActivity(new Intent(Home.this,Student.class));
}

EDIT

public class Home extends Activity {
private boolean spinnerAutoSelectionDone = false;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    String[] sp1 = getResources().getStringArray(R.array.Level);
    Spinner spinner = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sp1);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            if(!spinnerAutoSelectionDone){
                spinnerAutoSelectionDone = true;
                return;
            }

            String s = parent.getItemAtPosition(position).toString();
            if(s.equals("Admin"))
                startActivity(new Intent(Home.this,Admin.class));
            if(s.equals("Teacher"))
                startActivity(new Intent(Home.this,Student.class));   
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });    
}
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
1

use s.eauals() method instead of ==opetator.... because you are comparing for reference here not for values.

RajSharma
  • 1,941
  • 3
  • 21
  • 34