Spinner with a custom ArrayAdapter for objects that do not display the selected item

I have a custom ArrayAdapter for representing objects in a spinner control, I can load my objects list and show it for selection, but when the actual selection occurs, the counter does not show anything.

Operation code:

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 Element Template:

<?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"/>

Any ideas on why the selected item is not working? By the way, I also tried this with the usual ArrayAdapter with the same result.

Update . It looks like the view is being generated, but it is viewing the hierarchy viewer, the view does not get the rendering, measured / layout / drawing = n / a.

+5
source share
2 answers

, . spinner , notifyDataSetChanged(), spinner . , .

+4

getView return convertView; => return v;

0

All Articles