-1

Hi I am using an ArrayAdapter to display a default text in spinner and I don't understand the error

cannot resolve constructor ArrayAdapter(android.content.Context,int,java.lang.String[],java.lang.String,java.lang.String).

Can anyone tell me about this, please help me I don't know much about java coding.

 public class UneCon extends Fragment {

    private Spinner fromDetails, toDetails;
    private EditText fromInput, toInput;


    public static UneCon setArguments(int position) {
        UniqueConverter uniqueConverter = new UniqueConverter();
        Bundle args = new Bundle();
        args.putInt("position", position);
        uniqueConverter.setArguments(args);
        return uniqueConverter;
    }

 @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_uniquelayout, container, false);


        onInitVies(view);
        int position = getArguments().getInt("position");

        switch (position) {
            case 0:
                setAdapter(getResources().getStringArray(R.array.temperatureform), "Celsius", "Fahrenheit");

                break;
            case 1:
                setAdapter(getResources().getStringArray(R.array.weightform), "Kilograms", "Grams");

                break;

 return view;

    }


    private void setAdapter(String[] spinnerItems, String fromDefaultText, String toDefaultText) {

        fromDetails.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.spinner_item, spinnerItems, fromDefaultText, toDefaultText));//here it getting error//
    }

    private void onInitVies(View view) {
        fromDetails = (Spinner) view.findViewById(R.id.fromSpinner);
        toDetails = (Spinner) view.findViewById(R.id.toSpinner);

        fromInput = (EditText) view.findViewById(R.id.fromInput);
        toInput = (EditText) view.findViewById(R.id.toInput);
    }
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
sun
  • 1,832
  • 4
  • 19
  • 31

3 Answers3

0

Not sure what you want to do with fromDefaultText and toDefaultText. You don't need to pass that (unless you have specific use of it and using custom adapter). For now

Change

fromDetails.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.spinner_item, spinnerItems, fromDefaultText, toDefaultText));

to

fromDetails.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerItems));
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • I have two spinners so by using fromdefaultText and to, I'm setting default text to spinners – sun Mar 17 '16 at 11:06
  • @sun You dont need to pass those two values to `ArrayAdapter`. You can pass them to `setSpinner` method and take any action on them but dont pass in `ArrayAdapter`. Just replace you code with mine and it would work. – Rohit5k2 Mar 17 '16 at 11:11
  • Thank you @rohit then how can we pass default text than? – sun Mar 17 '16 at 11:15
  • @sun See this post. It has various ways to do that http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one – Rohit5k2 Mar 17 '16 at 11:20
0

Have you ever seen the Doc?

There are 6 types of constructor in ArrayAdapter class. And there are 2 constructors having maximum parameter of 4. And you are passing 5 parameters currently..

The 2 Constructors having max number of parameters are :

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

And

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

We are not sure what are you currently trying to do now. I suggest you to initialize ArrayAdapter transforming your current initialization into the above one..

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
-1

change getContext() to getActivity()

and following are four type of array adapter in android.

 ArrayAdapter(Context context, int resource)
Constructor
    ArrayAdapter(Context context, int resource, int textViewResourceId)
Constructor
    ArrayAdapter(Context context, int resource, T[] objects)
Constructor
    ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Constructor
    ArrayAdapter(Context context, int resource, List<T> objects)
Constructor
    ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)
Constructor 

hope this will help you.

Silvans Solanki
  • 1,267
  • 1
  • 14
  • 27