简体   繁体   English

从 android studio 中的微调器获取文本

[英]Getting text from a spinner in android studios

I have a spinner with 2 selections, "text1" and "text2".我有一个带有 2 个选项的微调器,“text1”和“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}".当我尝试获取“text1”并记录“text1”以查看我是否得到它时,我得到了这个:“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.您正在将对象转换为 String 这就是原因。

final String spinnerText = spinner.toString();

if you want to get the values from spinner use below one.如果您想从微调器中获取值,请使用低于 1 的值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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