具有自定义ArrayAdapter的微调器,用于不显示所选项目的对象 [英] Spinner with custom ArrayAdapter for objects not displaying selected item
问题描述
我有一个自定义ArrayAdapter来表示微调控件上的对象,我可以加载我的项目 列出并显示它以供选择,但是当实际选择发生时,微调框什么也不显示.
I have a custom ArrayAdapter to represent objects on a spinner control, I can load my items list and show it for selection, but when the actual selection happens the spinner shows nothing.
活动代码:
public MetroData metroData;
private Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
metroData = new MetroData();
spinner = (Spinner) findViewById(R.id.spinner1);
StopArrayAdapter dAdapter = new StopArrayAdapter(this, metroData.Stops);
spinner.setAdapter(dAdapter);
}
StopArrayAdapter:
StopArrayAdapter:
public class StopArrayAdapter extends ArrayAdapter<MetroStop> {
private List<MetroStop> items;
private Activity activity;
public StopArrayAdapter(Activity activity, List<MetroStop> items) {
super(activity, android.R.layout.simple_list_item_1, items);
this.items = items;
this.activity = activity;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
if (v == null) {
v = new TextView(activity);
}
v.setTextColor(Color.BLACK);
v.setText(items.get(position).getName());
return v;
}
@Override
public MetroStop getItem(int position) {
return items.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = activity.getLayoutInflater();
v = inflater.inflate(R.layout.view_spinner_item, null);
}
TextView lbl = (TextView) v.findViewById(R.id.text1);
lbl.setTextColor(Color.BLACK);
lbl.setText(items.get(position).getName());
return convertView;
}
}
微调视图项目模板:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#222"/>
关于所选项目视图为何不起作用的任何想法?顺便说一句,我也尝试过使用具有相同结果的普通ArrayAdapter.
Any ideas on why the selected item view doesn't work? Btw, I've also tried it with a normal ArrayAdapter with the same result.
更新:似乎已生成视图,但在层次结构查看器上查看时,该视图未呈现,已度量/布局/绘制= n/a".
Update Seems that the view gets generated but looking on the hierarchy viewer, the view is not getting rendered, Measured/Layout/Draw = n/a.
推荐答案
我发现了问题所在.由于我是从Internet上获取spinner
的数据的,所以我需要触发notifyDataSetChanged()
,即使没有此内容,spinner
的内容也会被更新.似乎所选的项目视图没有引起注意.
I found out what was the issue. Since I was fetching the data for the spinner
from the internet, I needed to trigger a notifyDataSetChanged()
, even though without this the contents of the spinner
were updated. It seems that the selected item view didn't get the notice.
这篇关于具有自定义ArrayAdapter的微调器,用于不显示所选项目的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!