java 视图对象转换_java-如何从onItemSelected()方法返回的视图对象...

onItemSelected()方法应该返回一个View作为其对象之一,在这种情况下,它是一个TextView,通过在Logcat中获取该对象的描述和哈希值进行了验证,因此该View实际上是一个TextView.通过此处显示的方法返回的视图

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

我如何获取存储在该视图中的文本字符串?例如,如果您这样做,

view.getText();

它应该返回存储在textView中的String,但是不起作用

但是我尝试了很多不同的事情,例如将视图强制转换为TextView,以从视图中获取存储的String,但这些都不起作用.我失败的尝试之一就是这样

((TextView) view).getText()

我如何从onItemSelected回调方法返回的视图中获取String?

ArrayList加载有Strings,并放入此处所示的适配器中,

public class SpinnerAdapter extends ArrayAdapter{

ArrayList objects;

Context context;

int textViewResourceId;

public SpinnerAdapter(Context context, int textViewResourceId, ArrayList objects) {

super(context, textViewResourceId, objects);

this.context = context;

this.textViewResourceId = textViewResourceId;

this.objects = objects;

}

spinnerOne.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override

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

// TODO Auto-generated method stub

Toast.makeText(StandardSelectionSettingsSmallTank.this, "id returned "+ Long.toString(id) , Toast.LENGTH_SHORT).show();

Toast.makeText(StandardSelectionSettingsSmallTank.this, "view returned "+ ((TextView) view).getText() , Toast.LENGTH_SHORT).show();

}

@Override

public void onNothingSelected(AdapterView> parent) {

// TODO Auto-generated method stub

}

});

}

编辑:我只是尝试了下面的代码,它正是在做我需要做的事情.它获取存储在微调器当前位置中的字符串.我之前使用ArrayList加载的字符串.这正在工作,所以我想我将使用此方法,而不使用onItemSelected方法返回的View对象.

字符串选择= spinnerOne.getSelectedItem().toString();

我将使用它,除非有人对如何使用视图对象有其他想法

这是什么问题FATAL EXCEPTION: main Process: com.example.lightcontrol_app2, PID: 4533 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lightcontrol_app2/com.example.lightcontrol_app2.ui.control.activity.EditingSingleLampActivity}: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2668) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1480) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6176) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) Caused by: java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner. at android.widget.Spinner.setOnItemClickListener(Spinner.java:571) at com.example.lightcontrol_app2.ui.control.activity.EditingSingleLampActivity.init(EditingSingleLampActivity.java:111) at com.example.lightcontrol_app2.ui.control.activity.EditingSingleLampActivity.onCreate(EditingSingleLampActivity.java:65) at android.app.Activity.performCreate(Activity.java:6692) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2729) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1480) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6176) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值