tags:

views:

79

answers:

2

Hi,

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

A: 

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();
      ....
   }
Mayra
+1  A: 

probably this code helps (you use either position or id to locate the item in your adapter)

spnCountries.setOnItemSelectedListener(spnCountriesListener);


private Spinner.OnItemSelectedListener spnCountriesListener =

  new Spinner.OnItemSelectedListener() {

    public void onItemSelected(AdapterView parent, View v, int position, long id) {

      Log.i("print", parent.getSelectedItem().toString());

    }



    public void onNothingSelected(AdapterView parent) { }              

};
Pentium10