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();
我将使用它,除非有人对如何使用视图对象有其他想法