简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM