Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

spinner drop down list onItemSelected

Can someone please show me the code for how to start a new activity from clicking one item on a spinner.

1 Answer

Jasmeet Singh
Jasmeet Singh
20,145 Points

I hope this helps.

Spinner Spinner = (Spinner) findViewById(R.id.spinner);

Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View view, int position, long row_id) {
        final Intent intent;
        switch(position){
            case 1:
                intent = new Intent(CurrentActivity.this, TargetActivity1.class);
                break;
            case 2:
                intent = new Intent(CurrentActivity.this, TargetActivity2.class);
                break;
// and so on 
// .....

        }
        startActivity(intent);

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

});