从微调框选择后,为什么不能跳到选定的新活动?

时间:2019-05-03 13:58:10

标签: java android button android-activity spinner

我正在尝试从微调器中选择一个国家,然后跳到相应的活动。例如:如果我在Main3Activity中选择微调器的“英国”,则跳至新的活动MainActivity。

但是我不知道为什么它不能跳跃。它可以选择,然后应用程序结束。

关于维护清单,我需要修改什么吗?

谢谢:)

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

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

<activity android:name=".Main3Activity" />
<activity android:name=".MainActivity" />
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="currency_array">
        <item>Australia</item>
        <item>Bitcorn</item>
        <item>Brazil</item>
        <item>China</item>
        <item>Egypt</item>
        <item>Europe</item>
        <item>Japan</item>
        <item>UK</item>
        <item>USA</item>
    </string-array>
</resources>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    Spinner currency = (Spinner) findViewById(R.id.currency);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currency_array, android.R.layout.simple_spinner_item);
    //specify which array to use for spinner

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //load array to spinner

    currency.setAdapter(adapter);

    //respond to the user


}//end method


@Override
public void onItemSelected (AdapterView < ? > adapterView, View view,int index, long l){

    String selected = adapterView.getItemAtPosition(index).toString();
    Toast.makeText(adapterView.getContext(), selected, Toast.LENGTH_SHORT).show();


    if(adapterView.getContext().equals("UK")){
        Intent intent = new Intent(Main3Activity.this, MainActivity.class);
        startActivity(intent);
    }


    /*if(index == 0){
        Intent intent = new Intent(Main3Activity.this, MainActivity.class);
        startActivity(intent);
    }
    */


}//end onItemSelected


public void onNothingSelected (AdapterView < ? > adapterView) {
} //end onNothingSelected

2 个答案:

答案 0 :(得分:1)

adapterView.getContext()替换为selected。您要检查项目文本是否等于UK,而不是Context,它与任何地方都不相关。

答案 1 :(得分:0)

尝试一下:

spinner.setOnItemSelectedListener(this);

或者:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
{
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String selectedItem = parent.getItemAtPosition(position).toString();
        switch(selectedItem) {
            case "UK":
                Intent intent = new Intent(Main3Activity.this, MainActivity.class);
                startActivity(intent);
                break;
            case "USA":
                // do something
                break;
        }
    }
    public void onNothingSelected(AdapterView<?> parent) {

    }           
});