Spinner没有选择任何项目,Spinner显示第一个项目的文本,而不是所选项目的文本

时间:2016-07-13 02:58:08

标签: android android-studio android-spinner

package com.example.pk.calculateage;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    Spinner S[];
    ArrayAdapter<String> aa[];
    Button b;

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

        ArrayList<String> year=new ArrayList<String>();

        for(int i=1950;i<2017;i++)
            year.add(String.valueOf(i));

        ArrayList<String> month=new ArrayList<String>();

        for(int i=1;i<13;i++)
            month.add(String.valueOf(i));

        ArrayList<String> date=new ArrayList<String>();

        for(int i=1;i<31;i++)
            date.add(String.valueOf(i));

        //arrayadapter
        aa=new ArrayAdapter[4];
        aa[0]=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,year);

        aa[0].setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        aa[1]=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,month);

        aa[1].setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        aa[2]=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,date);

        aa[2].setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //intialize spinner
        S=new Spinner[6];
        S[0]=(Spinner)findViewById(R.id.spinner1);
        S[1]=(Spinner)findViewById(R.id.spinner2);
        S[2]=(Spinner)findViewById(R.id.spinner3);
        S[3]=(Spinner)findViewById(R.id.spinner4);
        S[4]=(Spinner)findViewById(R.id.spinner5);
        S[5]=(Spinner)findViewById(R.id.spinner6);

        //to set adapter
        S[0].setAdapter(aa[0]);
        S[1].setAdapter(aa[1]);
        S[2].setAdapter(aa[2]);
        S[3].setAdapter(aa[0]);
        S[4].setAdapter(aa[1]);
        S[5].setAdapter(aa[2]);

        //to get values from the spinner
        final int dobyear=Integer.parseInt(S[0].getSelectedItem().toString());
        final int dobmonth=Integer.parseInt(S[1].getSelectedItem().toString());
        final int dobdate=Integer.parseInt(S[2].getSelectedItem().toString());
        final int cyear=Integer.parseInt(S[0].getSelectedItem().toString());
        final int cmonth=Integer.parseInt(S[1].getSelectedItem().toString());
        final int cdate=Integer.parseInt(S[2].getSelectedItem().toString());

        //Action on button
        b=(Button)findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                EditText et=(EditText)findViewById(R.id.editText);
                Toast.makeText(getApplicationContext(),"on Click",Toast.LENGTH_SHORT).show();
                if(dobyear>cyear)
                {
                    Toast.makeText(getApplicationContext(),"dob should be less then aur equal to current year",Toast.LENGTH_LONG).show();
                }
                else {                       
                      Toast.makeText(getApplicationContext(),String.valueOf(dobyear),Toast.LENGTH_LONG).show();
                      et.setText(String.valueOf(dobyear - cyear) + "years" + String.valueOf(dobmonth - cmonth) +
                      "month" + String.valueOf(dobdate - cdate) + "days");
            }
        }
    });

    }
}

Spinner没有选择任何项目,Spinner显示第一个项目的文本,而不是所选项目的文本。 它总是在旋转器中显示1950年,第1个月和第1天

我该如何解决这个问题?

0 个答案:

没有答案