Spinner if else声明

时间:2013-08-16 04:45:43

标签: java android if-statement spinner

我想为Spinner做if else声明。 我宣布“六月”,“七月”,“八月”,“九月”,“八月”,“新闻”,“十二月”

当我点击“八月”时,它会给我MainActivity.java 否则它会给我August.java。 马上, 我的微调器是自动选择, 这意味着,当我加载页面时,它将自动为我选择“june”。 有没有办法让我禁用自动选择?

这是我的代码

String [] months = {
                "June",
                "July",
                "August",
                "Septemeber",
                "November",
                "December",

我的date.java

的一部分
//SpinnerView
            s1 = (Spinner) findViewById(R.id.spinner1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, months);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
                int index = s1.getSelectedItemPosition();
                //Toast.makeText(getBaseContext(), "You have seleted item :" + months[index] , Toast.LENGTH_SHORT).show();
            }
            public void onNothingSelected(AdapterView<?>arg0) {}
            });


    if ( s1.equals("August")) {

        startActivity(new Intent(date.this,MainActivity.class));
    }
    else{
        startActivity(new Intent(date.this,august
                .class));

    }

3 个答案:

答案 0 :(得分:1)

试试这个:

添加“Select Month”,“june”,“july”,“august”,“sept”,“oct”,“nov”,“dec”等数组

并检入

s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
                int index = s1.getSelectedItemPosition();
                if(index>0)
                {
                             String Month = MonthArray[index];
                    if ( Month.equalIgnoreCase("August")) {

                        startActivity(new Intent(date.this,MainActivity.class));
                    }
                    else{
                        startActivity(new Intent(date.this,august
                                .class));

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

希望它能帮助!!

答案 1 :(得分:1)

可能会有所帮助

    import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class date extends Activity implements AdapterView.OnItemSelectedListener{



    ArrayList<String> months;

    Spinner spinner;

    ArrayAdapter<String> month_adapter;

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


        months = new ArrayList<String>();

        months.add("Select");
        months.add("June");
        months.add("July");
        months.add("August");
        months.add("Septemeber");
        months.add("November");
        months.add("December");

        spinner = (Spinner) findViewById(R.id.spinner); 
        spinner.setOnItemSelectedListener(this);


        month_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, months);
        month_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(month_adapter);


    }

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

    @Override
    public void onItemSelected(AdapterView<?> arg0, View v, int position,
            long arg3) {
        // TODO Auto-generated method stub


        if(!(spinner.getSelectedItem().toString().trim().equals("Select")))
        {

            if ( spinner.getSelectedItem().toString().trim().equals("August")) {

                startActivity(new Intent(date.this,MainActivity.class));
            }
            else{
                startActivity(new Intent(date.this,august
                            .class));

            }

        }

    }

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

    }

}

答案 2 :(得分:0)

您需要将Spinner的最后一个选定项目位置存储在您的数据库或SharedPreference中。加载您的应用程序后,请检查您的数据库或SharedPreference中是否有项目位置,然后根据此位置选择Spinner中的项目。