1

I need to set the selected item on a spinner using the value of a String. At the moment I use this (hint found here: Set selected item of spinner programmatically).

Spinner nudSpinner = (Spinner)nud.findViewById(R.id.spinUnity);
EditText nudEText = (EditText)nud. findViewById(R.id.etxtUnityName);
nudSpinner.setSelection(((ArrayAdapter<String>)nudSpinner.getAdapter()).getPosition(unities.get(position).split(" ")[0]));

It works fine but it gives me the following warning:

Unchecked cast: 'android.widget.SpinnerAdapter' to 'android.widget.ArrayAdapter<java.lang.String>'
Signals places where an unchecked warning is issued by the compiler, for example:

  void f(HashMap map) {
    map.put("key", "value");
  }

Hint: Pass -Xlint:unchecked to javac to get more details.

How can I solve the warning?

Thanks

MarcoF
  • 53
  • 5
  • Possible duplicate of [Type safety: Unchecked cast](https://stackoverflow.com/questions/262367/type-safety-unchecked-cast) – Dima Kozhevin Nov 10 '17 at 12:32
  • 1
    But you set it first with `nudSpinner.setAdapter(adapter)` .... so why didn't you store reference to it (`adapter`)? If you do this there would be no need for cast at all. – Selvin Nov 10 '17 at 12:38
  • @Selvin I don't set the adapter... – MarcoF Nov 10 '17 at 12:55
  • if you did not set the adapter so why do you want to call `getAdapter()` at all? and whats the point in calling: `getPosition(unities.get(position)`? – pskink Nov 10 '17 at 14:41

0 Answers0