Android 如何将活动链接到微调器的元素?

Android 如何将活动链接到微调器的元素?,android,android-spinner,Android,Android Spinner,我正在创建代码中给出的微调器,如下所示。如何将它连接到另一个活动(比如Bangalore.java)。我尝试了stackoverflow上可用的东西,但它不起作用 package com.example.searchbox; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.widget.ArrayAdapter; import android.widge

我正在创建代码中给出的微调器,如下所示。如何将它连接到另一个活动(比如Bangalore.java)。我尝试了stackoverflow上可用的东西,但它不起作用

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;
    }

}
package com.example.searchbox;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner;
导入com.example.searchbox.run;
导入com.example.searchbox.ArrayList;
公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
字符串[]cityNames={“斋浦尔”、“班加罗尔”、“阿格拉”};
setContentView(R.layout.activity_main);
微调器微调器=(微调器)findViewById(R.id.spinner1);
spinner.setAdapter(新阵列适配器)(此,
android.R.layout.simple\u list\u item\u 1,
城市名称);
/*如何将微调器的活动连接到另一个活动*/
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}

我会使用OnItemSelectedListener,然后在捕捉到正确的项目后启动活动。

您可以执行以下操作:

    MyOnItemSelectedListener myonitemselectedlistener =new MyOnItemSelectedListener (savedRoomNames);
    YOURSPINNER.setOnItemSelectedListener(myonitemselectedlistener);
然后:

    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) {
    }
私有类MyOnItemSelectedListener实现OnItemSelectedListener{
AdapterView arg0;
视图arg1;
int-arg2;
长arg3;
公共MyOnItemSelectedListener(字符串[]gespeicherteRaeume){
savedRooms=gespeicherteRaeume;
}
@凌驾
已选择公共视图(适配器视图arg0、视图arg1、内部arg2、,
长arg3){
这个.arg0=arg0;
arg1=arg1;
arg2=arg2;
arg3=arg3;
//在这里,您必须查看哪个项目是arg2,然后如果==yy,您的项目将通过intent启动您的新活动
}
未选择公共无效(AdapterView arg0){
}

从spinner获取所选项目,如果我正确理解您的问题,请使用意图将值传递给下一个活动