4

I have two spinner item. One is my Day spinner , another is my Month Spinner. If I select month February from month spinner and if I select day as 30 ,it should not be done.

Another example: month April has 30 days , so if someone selects Month as April and day as 31st ,then it is not correct. Please tell me how to do get the value of one spinner corresponding to the value of another spinner.

Here is my code where I have the two spinners:

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 /*************day spinner**********/

        Spinner day  = (Spinner) findViewById(R.id.spinner2);
        ArrayAdapter<CharSequence> dayadapter = ArrayAdapter.createFromResource(
                this, R.array.item_day, android.R.layout.simple_spinner_item);
        dayadapter.setDropDownViewResource(R.layout.spinner_layout);
        day.setAdapter(dayadapter);

        /*****************month spinner**********************/


        Spinner month  = (Spinner) findViewById(R.id.spinner3);
        ArrayAdapter<CharSequence> monthadapter = ArrayAdapter.createFromResource(
                this, R.array.item_month, android.R.layout.simple_spinner_item);
        monthadapter.setDropDownViewResource(R.layout.spinner_layout);
        month.setAdapter(monthadapter);


}}

My Xml where the spinners are defined :

    <Spinner
            android:id="@+id/spinner2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/spinner3"
            android:layout_alignTop="@+id/textView4"
            android:layout_toLeftOf="@+id/textView3" />
   <Spinner
            android:id="@+id/spinner3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/spinner2"
            android:layout_toLeftOf="@+id/textView3"
            android:layout_toRightOf="@+id/textView5" />

my spinner_layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/spinnerTarget"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="#000000"
          android:textSize="12pt"
/>

my day array

<string name="day_picker">Select an iten</string>
<string-array name="item_day">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
     <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
    <item>11</item>
    <item>12</item>
     <item>13</item>
    <item>14</item>
    <item>15</item>
    <item>16</item>
    <item>17</item>
    <item>18</item>
     <item>19</item>
    <item>20</item>
    <item>21</item>
    <item>22</item>
    <item>23</item>
    <item>24</item>
     <item>25</item>
    <item>26</item>
    <item>27</item>
    <item>28</item>
    <item>29</item>
    <item>30</item>
    <item>31</item>

</string-array>

my month array

<resources>
    <string name="month_picker">Select an item</string>
    <string-array name="item_month">
        <item>January</item>
        <item>February</item>
        <item>March</item>
        <item>April</item>
        <item>May</item>
        <item>June</item>
         <item>July</item>
        <item>August</item>
        <item>September</item>
        <item>October</item>
        <item>November</item>
        <item>December</item>
         </string-array>
</resources>

If I select any month I want to get corresponding no. of days automatically in day-spinner Note: I do not want any error message after clicking a button. thanks in advance.

4

2 回答 2

2

这真的很简单。将 OnItemSelectedListener() 放在月份微调器上,添加您自己的侦听器,用正确的值初始化日期微调器

于 2012-05-02T10:07:13.207 回答
1
month.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {
            // TODO Auto-generated method stub
            List<String> s = Arrays.asList(getResources().getStringArray(R.array.item_day));
            if (pos == 0 || pos == 2 || pos == 4 || pos == 8 || pos == 9
                    || pos == 11) {
                ArrayAdapter<String> dayadapter = new  ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
                day.setAdapter(dayadapter);
            } else if (pos == 1) {
                s = s.subList(0,28);                    
                ArrayAdapter<String> dayadapter = new  ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
                day.setAdapter(dayadapter);
            } else {
                s = s.subList(0,30);                    
                ArrayAdapter<String> dayadapter = new  ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
                day.setAdapter(dayadapter);
            }
        }

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

        }
    });
于 2012-05-02T10:51:44.297 回答