Oyubu Obukohwo
Oyubu Obukohwo

Reputation: 45

how to launch new activity based on spinner value

i am building this app that has a spinner and button on the first activity, but i have other activities which are lauched based on spinner value. but the problem now is when i click on button nothing happens, what could be wrong? this is my code

public class ow extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ow);
       Button button1=(Button) findViewById(R.id.tac);

        Spinner spin = (Spinner) findViewById(R.id.spinner);
        final String text = spin.getSelectedItem().toString();
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (text.equals("GTB")) {
                    Intent intent = new Intent(ow.this, gtb.class);
                    startActivity(intent);
                }else if(text.equals("ZENITH")){
                    Intent intent=new Intent(v.getContext(),z.class);
                    startActivity(intent);
                }
            }

        });

    }
}

Upvotes: 0

Views: 1466

Answers (4)

Danilo Silva
Danilo Silva

Reputation: 171

U have to create a spinner event.

For example:

    spinner = (Spinner) findViewById(R.id.spinner);
    adapter = ArrayAdapter.createFromResource(this,
    R.array.opcoes,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, 
        View view, int position, long l) {

            String item = spinner.getSelectedItem().toString();

   Switch(item){
   case item1:
   Intent yourAcitivity = new Intent(this, yourAcitivity.class);
            startActivity(yourAcitivity );
   case item2:
   Intent yourSecondAcitivity = new Intent(this, yourSecondAcitivity.class);
            startActivity(yourSecondAcitivity);
    //Keep adding items.

}

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

Upvotes: 0

Pratik Butani
Pratik Butani

Reputation: 62419

You have to write following line on button click listener

final String text = spin.getSelectedItem().toString();

like:

    button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = spin.getSelectedItem().toString();

                if (text.equals("GTB")) {
                    Intent intent = new Intent(ow.this, gtb.class);
                    startActivity(intent);
                }else if(text.equals("ZENITH")){
                    Intent intent=new Intent(v.getContext(),z.class);
                    startActivity(intent);
                }
            }

        });

Hope it will works.

Upvotes: 0

mr mcwolf
mr mcwolf

Reputation: 2859

Try this

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_ow);
       Button button1=(Button) findViewById(R.id.tac);

        final Spinner spin = (Spinner) findViewById(R.id.spinner);  
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = spin.getSelectedItem().toString();

                if (text.equals("GTB")) {
                    Intent intent = new Intent(ow.this, gtb.class);
                    startActivity(intent);
                }else if(text.equals("ZENITH")){
                    Intent intent=new Intent(v.getContext(),z.class);
                    startActivity(intent);
                }
            }

        });

    }}

Upvotes: 1

JavaGhost
JavaGhost

Reputation: 406

Your OnClickListener is Anonymous type. The string value is supplied to it in onCreate which is default value, probably null. Use spinner as class member and access selected item value inside onClick method.

Upvotes: 0

Related Questions