user1390816
user1390816

Reputation: 623

The constructor ArrayAdapter<String>(LayoutNext, int, String[]) is undefined

I try to implement the items to a Spinner from a android.support.v4.app.Fragment class. I always get the compiler Error

The constructor ArrayAdapter(LayoutNext, int, String[]) is undefined

How can I fix this? Here is the code:

    public class LayoutNext extends Fragment 
                            implements OnClickListener,OnItemSelectedListener{

    TimePicker timepicker;
    private Spinner spinner_next;
    public ArrayAdapter<String> adapter;

    public static Fragment newInstance(Context context) {
        LayoutNext f = new LayoutNext();    

        return f;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_next, null);  

        timepicker = (TimePicker) root.findViewById(R.id.timePicker1);
        timepicker.setIs24HourView(true);
        timepicker.setCurrentHour(0);
        timepicker.setCurrentMinute(0);

        String[] items_next = { "Next", "From to"};
        spinner_next = (Spinner) root.findViewById(R.id.sp_next);
        spinner_next.setOnItemSelectedListener(this);

 // Here I get the error
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_spinner_item, 
                items_next);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_next.setAdapter(adapter);

        return root;
    }

Upvotes: 1

Views: 2896

Answers (1)

Sam
Sam

Reputation: 86948

Unlike Activities, Fragments are not subclasses of Context so you cannot use this where a Context is required. Simply use getActivity() instead.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getActivity(),
        android.R.layout.simple_spinner_item, 
        items_next);

Upvotes: 5

Related Questions