Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 863731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:21:38+00:00 2026-05-15T09:21:38+00:00

I am trying to populate a TextView based on the current selected options in

  • 0

I am trying to populate a TextView based on the current selected options in 3 Spinner(s) but cant seem to figure out how to retrieve the selected values from the Spinners to invoke the update function with.
Here is my current code (quite messy but I’m just learning Java :)),

public class AgeFun extends Activity {
private String[] dayNames;
private String[] yearArray;
private final static int START_YEAR = 1990;
private static TextView textDisp;
private Button calcButton; 
private static Spinner spinnerDay, spinnerYear, spinnerMonth;
private static ArrayAdapter<?> monthAdapter, dayAdapter, yearAdapter;
private int year, month, day;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    year = 2000;
    month = 1;
    day = 1;
    textDisp = (TextView) findViewById(R.id.textView1);
    calcButton = (Button) findViewById(R.id.button);
    calcButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            AgeFun.updateAge(year, month, day);
        }
    });

    // Month spinner
    spinnerMonth = (Spinner) findViewById(R.id.spinnerFirst);
    monthAdapter = ArrayAdapter.createFromResource(
            this, R.array.monthList, android.R.layout.simple_spinner_item);
    monthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerMonth.setAdapter(monthAdapter);

    // Day spinner
    dayNames = new String[31];
    for(int i =1; i <= 31; ++i)
    {
        dayNames[i-1] = Integer.toString(i);
    }
    spinnerDay = (Spinner) findViewById(R.id.spinnerSecond);
    dayAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, dayNames);
    spinnerDay.setAdapter(dayAdapter);

    // Year spinner
    yearArray = new String[40];
    for(int i =0; i < 40; ++i)
    {
        yearArray[i] = Integer.toString(START_YEAR+i);
    }
    spinnerYear = (Spinner) findViewById(R.id.spinnerThird);
    yearAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, yearArray);
    spinnerYear.setAdapter(yearAdapter);

    updateAge(2000,1,1);
}

private static void updateAge(int year, int month, int day) {
    Date dob = new GregorianCalendar(year, month, day).getTime();
    Date currDate = new Date();

    long age = (currDate.getTime() - dob.getTime()) / (1000 * 60 * 60 * 24) / 365;
    textDisp.setText("Your are " + Long.toString(age) + " years old");
}

}

Any help with this would be great.

TIA

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:21:39+00:00Added an answer on May 15, 2026 at 9:21 am

    You can get the current spinner value with getSelectedItem. Since you filled your array adapters with CharSequence, that is the type the selected item would be.

    Alternatively, you should be able to use an ArrayAdapter and deal with ints directly. I believe that the spinner used the toString method of the object type it is provided.

    Edit: I missed the important part, add an onItemSelected listener to each of the spinners, i.e:

        AdapterView.OnItemSelectedListener listener = new AdapterView.OnItemSelectedListener () {
            onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               updateAge();
            }
        }
        dayAdapter.setOnItemSelectedListener(listener);
        ....
    
       private void updateAge() {
          int day = spinnerDay.getSelectedItem();
          ....
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.