Skip to content Skip to sidebar Skip to footer

How To Update A Textview On Buttonclick With Spinner(s) Values

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

Solution 1:

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

spnCountries.setOnItemSelectedListener(spnCountriesListener);


private Spinner.OnItemSelectedListenerspnCountriesListener=newSpinner.OnItemSelectedListener() {

    publicvoidonItemSelected(AdapterView parent, View v, int position, long id) {

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

    }



    publicvoidonNothingSelected(AdapterView parent) { }              

};

Solution 2:

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);
    ....

   privatevoidupdateAge() {
      int day = spinnerDay.getSelectedItem();
      ....
   }

Post a Comment for "How To Update A Textview On Buttonclick With Spinner(s) Values"