Search code examples
javaandroid-studiospinner

Getting text from a spinner in android studios


I have a spinner with 2 selections, "text1" and "text2". When I tried to get "text1" and log "text1" to see if I got it or not, I got this instead: "androidx.appcompat.widget.AppCompatSpinner{d963757 VFED..CL. ........ 389,1177-883,1230 #7f080094 app:id/spinner1}".

What am I doing wrong?

String getSpinnerName = "spinner" + "1";
Spinner spinner =  (Spinner) findViewById(getResources().getIdentifier(getSpinnerName, "id", getPackageName()));
final String spinnerText = spinner.toString();
Log.d(String.valueOf(LOG), spinnerText);

Solution

  • you are converting an object into String that's why.

    final String spinnerText = spinner.toString();
    

    if you want to get the values from spinner use below one.

    final String spinnerText = spinner.getSelectedItem().toString();