6

what is wrong with this? see code and warning message, can anyone elaborate.

public void chooseBreakfast() {

            ArrayAdapter planAdapter1 = new ArrayAdapter(this,
            android.R.layout.simple_spinner_item, new Food[]{
            new Food(1, "Toast"),
            new Food(99, "Cereal"),
            new Food(53, "Fruit"),
            new Food(153, "Yogurt")
     });

WARNING MESSAGE - Unchecked call to 'ArrayAdapter(Context, int, T[])' as a member of raw type 'android.widget.ArrayAdapter' less... (Ctrl+F1)

JDK 5.0 only. Signals places where an unchecked warning is issued by the compiler, for example:

void f(HashMap map) {
      map.put("key", "value");
}
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
ERN
  • 115
  • 1
  • 8
  • possible duplicate of [unchecked call to ArrayAdapter](http://stackoverflow.com/questions/4204706/unchecked-call-to-arrayadapter) – mmmmmpie Feb 16 '15 at 13:03

1 Answers1

9

The compiler is warning you about the usage of a raw ArrayAdapter. You should use a generic ArrayAdapter as

ArrayAdapter<Food> planAdapter1 = new ArrayAdapter<Food>(this,
        android.R.layout.simple_spinner_item, new Food[]{
        new Food(1, "Toast"),
        new Food(99, "Cereal"),
        new Food(53, "Fruit"),
        new Food(153, "Yogurt")
});

Notice, how the ArrayAdapter class and its constructor are defined.

public class ArrayAdapter<T> extends BaseAdapter ... { // T = generic type

    public ArrayAdapter(Context context, int resource, T[] objects) {
        init(context, resource, 0, Arrays.asList(objects));
    }
    ...
}

They use a generic type parameter T which the simple ArrayAdapter() initialization ignores to pass. Hence, the warning. The benefit of passing the type T is that you get rid of unnecessary casts and your code becomes more type safe.

For e.g., calling ArrayAdapter#getItem() on a raw adapter will return Object and require you to cast it into Food yourself. The generic adapter will return the actual type Food automatically.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • Don't know if it's related, but now i've changed that and went to run emulated i'm seeing LogCat errors. 02-16 13:45:55.230 837-837/com.example.student.neillapp E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.student.neillapp/com.example.student.neillapp.AddActivity}: java.lang.NullPointerException – ERN Feb 16 '15 at 13:47
  • This can be a side effect from how your current code was interacting with the raw adapter but certainly has nothing to do with the original question: the compiler warning. So, please post a new question with complete stack trace and AddActiv‌​ity code specially around the lines mentioned in the exception. Post a link here in the comments, so I can a take a look as well. – Ravi K Thapliyal Feb 16 '15 at 13:55
  • http://stackoverflow.com/questions/28543514/catlog-errors-stopping-application-from-running-code-added – ERN Feb 16 '15 at 14:22