具有自定义ArrayAdapter的微调器,用于不显示所选项目的对象

时间:2012-05-21 01:04:31

标签: android view spinner android-arrayadapter selected

我有一个自定义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;
}
}

Spinner视图项模板:

<?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,结果相同。

更新似乎视图已生成但在层次结构查看器上查看,视图未呈现,Measured / Layout / Draw = n / a。

2 个答案:

答案 0 :(得分:4)

我发现了什么问题。由于我是从互联网上获取spinner的数据,因此我需要触发notifyDataSetChanged(),即使没有这个,spinner的内容也会更新。似乎所选项目视图没有得到通知。

答案 1 :(得分:0)

在getView return convertView; => return v;