没有按钮的Spinner的OnItemListener

时间:2014-08-30 16:25:20

标签: java android spinner onitemselectedlistener

我的问题如下:我有一个StartActivity,用户可以在其中找到一个微调器,并可以从数组中的选项列表中进行选择。我希望用户可以选择,然后跳转到另一个活动,例如用户选择" Wien Simmering"然后打开Simmering.java。

package at.co.ccc.mondel;

import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.os.Bundle;

public class StartingPoint extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Spinner spinner = (Spinner) findViewById(R.id.comboCasino);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // action with ID action_refresh was selected
        case R.id.action_refresh:
        Toast.makeText(this, R.string.refreshed, Toast.LENGTH_SHORT).show();
        break;
        // action with ID action_settings was selected
        case R.id.action_settings:
        Toast.makeText(this, R.string.settings_clicked, Toast.LENGTH_SHORT).show();
        break;
        default:
        break;
        }
        return true;
      } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.starting_point, menu);
        return true;
    }
}

这是字符串xml:

<string-array name="comboCasino">
    <item>Wien Simmering</item>
    <item>Wien Lugner City</item>
    <item>Gmunden</item>
    <item>Salzburg</item>
    <item>Linz</item>
    <item>Saalbach</item>
    <item>Innsbruck</item>
    <item>Reutte</item>
    <item>Bregenz</item>
    <item>Kufstein</item>
    <item>Bratislava</item>
</string-array>

1 个答案:

答案 0 :(得分:0)

package at.co.ccc.mondel;

import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;

public class StartingPoint extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Spinner spinner = (Spinner) findViewById(R.id.comboCasino);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        // this will be called when you select any item in this spinner
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
                // get the text at that position
                String Text = spinner.getSelectedItem().toString();
                // if Text is equals to "Wien Simmering"
                if(Text.equals("Wien Simmering")) {
                    // start a new activity
                    // here StartingPoint.this is context
                    // and simmering.class is the next activity going to be opened
                    Intent NewPost = new Intent(StartingPoint.this,Simmering.class);
                    startActivity(NewPost);
                }
            }

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

            }
        } ); // i forget to add ); here that is why it was giving multiple markers error
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // action with ID action_refresh was selected
    case R.id.action_refresh:
    Toast.makeText(this, R.string.refreshed, Toast.LENGTH_SHORT).show();
    break;
    // action with ID action_settings was selected
    case R.id.action_settings:
    Toast.makeText(this, R.string.settings_clicked, Toast.LENGTH_SHORT).show();
    break;
    default:
    break;
    }
    return true;
    } 

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.starting_point, menu);
        return true;
    }

}