微调与不显示所选项目对象的自定义ArrayAdapter(Spinner with custom A

2019-09-17 09:22发布

我有一个自定义ArrayAdapter代表在旋转器上的控制对象,我可以载入我的项目名单,并显示它的选择,但在实际选择发生微调显示什么。

活动代码:

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:

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试了一下。

更新似乎视图获取生成的,但找上了层次观众的观点是没有得到呈现,测量/规划/绘制= N / A。

Answer 1:

我发现出了什么问题。 自从我获取的数据spinner从互联网,我需要触发notifyDataSetChanged()即使没有这个内容spinner进行了更新。 看来,所选的项目视图没有得到通知。



Answer 2:

在getView return convertView; => return v;



文章来源: Spinner with custom ArrayAdapter for objects not displaying selected item