如何在Spinner中选择项目以打开新活动

时间:2016-02-18 12:29:44

标签: java android android-spinner

我正在尝试在Spinner中选择一个项目,当我点击发送按钮时该项目将打开一个活动。例如,活动1和活动2.在我的微调器中,我有项目1和项目2.当我选择项目1时,我希望活动1打开。  我尝试了一些代码,但它没有用。  这是我的代码。

activity_main.xml中的Spinner

<Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_alignBottom="@+id/button"
        android:layout_toRightOf="@+id/button"
        android:layout_toEndOf="@+id/button"
        android:entries="@array/punpColleges"
        android:spinnerMode="dropdown" />

在我的MainActivity.java

public class MainActivity extends ActionBarActivity {
    Spinner spin = (Spinner) findViewById(R.id.spinner);
    private static Button button_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OnClickButtonListener();
    }
    public void OnClickButtonListener() {

        button_send = (Button) findViewById(R.id.button);
        button_send.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        if (spin.getSelectedItem().toString().equals("CCS")) {
                            startActivity(new Intent(MainActivity.this, ListActivity.class));
                        } else {
                            startActivity(new Intent(MainActivity.this, SecondTesting.class));
                        }
                    }
                }
        );

}

字符串位于strings.xml

<string-array name="punpColleges">
        <item>CCS</item>
        <item>CBE</item>
    </string-array>

我还在AndroidManifest.xml中添加了这个

<intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ListActivity"
            android:label="@string/title_activity_list" >
            <intent-filter>
                <action android:name="android.intent.action.ListActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

单击按钮,尝试此操作来调用您的活动

try {
        myClass = Class.forName("com.example.yourPackageName." + classComplete); //classcomplete as your file name, take it from spinner selected object
        Intent myIntent = new Intent(MainActivity.this, myClass);
        startActivity(myIntent);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

答案 1 :(得分:0)

您无法在班级初始化您的微调器。必须在致电setContentView后完成。除非您已将布局设置为活动,否则无法使用findViewById

像这样定义微调器

Spinner spin;

添加

spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.simple_spinner_dropdown_item,
             getResources().getStringArray(R.array.punpColleges));
spin.setAdapter(spinnerAdapter);
致电onCreate

后,在setContentView

答案 2 :(得分:0)

定义微调器和变量以存储微调器的位置

int x=0;
Spinner spin=(Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, 
         android.R.layout.simple_spinner_dropdown_item,
         list);
spin.setAdapter(spinnerAdapter);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent,
                                   View view, int position, long id) {
                                                x=position;
 });
}

现在比较按钮上的位置

if(x==0)
 Intent myIntent = new Intent(MainActivity.this, myClass1);
    startActivity(myIntent);
else
Intent myIntent = new Intent(MainActivity.this, myClass2);
    startActivity(myIntent);