Question

I am creating a spinner given in the code as follow. How can i connect it to another activity(say Bangalore.java). I tried the something available on stackoverflow but its not working.

package com.example.searchbox;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.example.searchbox.run;
import com.example.searchbox.ArrayList;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        String[] cityNames = {"Jaipur","Bangalore","Agra"};
        setContentView(R.layout.activity_main);
        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        spinner.setAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,
                    cityNames));
        /*how can i connect activity of the spinner to another activity*/

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Was it helpful?

Solution

You can do something like this:

    MyOnItemSelectedListener myonitemselectedlistener =new MyOnItemSelectedListener (savedRoomNames);
    YOURSPINNER.setOnItemSelectedListener(myonitemselectedlistener);

And then:

    private class MyOnItemSelectedListener implements OnItemSelectedListener{
    AdapterView<?> arg0;
    View arg1;
    int arg2;
    long arg3;

    public MyOnItemSelectedListener(String[] gespeicherteRaeume) {
        this.savedRooms=gespeicherteRaeume;
    }
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        this.arg0=arg0;
        this.arg1=arg1;
        this.arg2=arg2;
        this.arg3=arg3;

        //here you have to look which item is arg2 and then if == yyour item start your new activity via intent

    }
    public void onNothingSelected(AdapterView<?> arg0) {
    }

OTHER TIPS

I would use an OnItemSelectedListener and then start the activity once the correct item has been caught.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top