Steven
Steven

Reputation: 833

Android Spinner add/remove entries

I am creating an android app that asks a user their birth day and their birth month. I am using two spinners (one for the month and one for the days of the month). I want the spinner for the days to change based on the month, for example February has 29 days, so I want the day spinner to stop at the number 29. But I want the day spinner to go up as high as 31 if a month with 31 days is selected.

What is the best way to do this? I know I can use DatePicker, but I have never used it before and I only need a month and day (not the year).

debugText is just a textView that I created to test my spinner and make sure my coding was right so far.

//code

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.form);

    // Month

    monthSpinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> monthAdapter = ArrayAdapter
            .createFromResource(this, R.array.month,
                    android.R.layout.simple_spinner_item);
    monthAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_item);
    monthSpinner.setAdapter(monthAdapter);

    monthName = "January";

    // Day

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

    debugText = (TextView) findViewById(R.id.debugText);
    debugText.setText("Jaunary");

    daySpinner = (Spinner) findViewById(R.id.spinner2);

    monthSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

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

            int monthPos = monthSpinner.getSelectedItemPosition();
            switch (monthPos) {
            case 0:
                debugText.setText("January");

                break;
            case 1:

                debugText.setText("February");

                break;
            case 2:
                debugText.setText("March");

                break;
            case 3:
                debugText.setText("April");

                break;
            case 4:
                debugText.setText("May");

                break;

            case 5:
                debugText.setText("June");

                break;
            case 6:

                debugText.setText("July");

                break;
            case 7:
                debugText.setText("August");

                break;
            case 8:
                debugText.setText("September");

                break;
            case 9:
                debugText.setText("October");

                break;

            case 10:
                debugText.setText("November");

                break;
            case 11:
                debugText.setText("December");

                break;
            }

        }

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

        }
    });

}

Upvotes: 2

Views: 671

Answers (1)

azgolfer
azgolfer

Reputation: 15137

Really the DatePicker is the easiest way to do this, how else would you account for leap year to show the extra day on Feb? But if you insist on Spinners, here's how I would do it:

  1. When the view is first shown, disable the daySpinner, and populate the monthSpinner and set the selection to nothing.
  2. In the OnItemSelected listener of monthSpinner, once it's changed to a month, enable the daySpinner and based on the month selected, populate the daySpinner with the correct ArrayAdapter of days (e.g. you would have 4 arrays of day 1 to 28, 1 to 29, 1 to 30, 1 to 31) and set the adapter to daySpinner.
  3. When user changes the monthSpinner selection, you have to rebuild the daySpinner again.

Finally, when OK button is clicked, just pick the month and day from the spinners.

Upvotes: 1

Related Questions