dtcDev
dtcDev

Reputation: 3

Android Spinner doesn't display items

I had very simple xml:

    <Spinner
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/spinner"
            android:layout_marginTop="15dp">

            </Spinner>
</RelativeLayout>

and simple java code (copied from android docs and little modified by stackoverflow answers):

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_onecarinfo, container, false);
        spinner = (Spinner) view.findViewById(R.id.spinner);
        buildCarsListState();
        return inflater.inflate(R.layout.fragment_onecarinfo, container, false);
    }

    //fill car numbers list and set them to spinner source
    private void buildCarsListState() {
        List<String> carNumbers = new ArrayList<String>();

        //test
        carNumbers.add("test1");
        carNumbers.add("test2");



        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this.getActivity(), android.R.layout.simple_spinner_item, carNumbers);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

But, when I start debug this idiot-like simplest example, my spinner is empty. 2 hours to find reason - I realy no idea...

Help!

Upvotes: 0

Views: 977

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006664

You are correctly setting up the Spinner. Then you are throwing it away.

Replace:

return inflater.inflate(R.layout.fragment_onecarinfo, container, false);

with:

return view;

Upvotes: 2

Related Questions