Harry Adams
Harry Adams

Reputation: 487

Spinner not filled with ArrayAdapter

So my spinner isn't being filled programatically:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_first_boot__language_selection, container, false);
    setupSpinner(view, R.id.firstLanguageSpinner, R.array.languagesSpinner);
    setupSpinner(view, R.id.secondLanguageSpinner, R.array.languagesSpinner);
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_first_boot__language_selection, container, false);
}

private void setupSpinner(View view, int spinnerResourceId, int arrayResourceId) {
    Spinner spinner = view.findViewById(spinnerResourceId);
    spinner.setSelection(1);

    //Adapts strings into CharSequence.
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this.getContext(), arrayResourceId, android.R.layout.simple_spinner_item);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}

However I cannot for the life of me figure out why not? I would fill it via XML, however this code doesn't include other functions that I had added. For instance, in setupSpinner, I had added the onClickListener event, which wasn't working.

Upvotes: 2

Views: 86

Answers (1)

lionscribe
lionscribe

Reputation: 3513

In onCreateView you should be using return view;, not return inflater.inflate... What you are doing is inflating a second copy of the fragment, which is not initialized with spinner data.

Upvotes: 4

Related Questions