0

In Android I was practicing recycling view and I notice when i am contverting an String type array into Integer type list it is working fine as well as when i am putting string type list into Interger type ArrayAdapter it is working fine.

 String[] words = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};

 List<Integer> listOfWords = new ArrayList(Arrays.asList(words));

ArrayAdapter<Integer> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,listOfWords);

ListView listView = (ListView) findViewById(R.id.list);

listView.setAdapter(adapter);
  • 1
    You are using a raw type (for example `new ArrayList()` instead of `new ArrayList`). Those effectively break the generic type system and shouldn't be used. – Joachim Sauer Apr 22 '21 at 18:25
  • but there reference is not generic List listOfWords @Joachim Sauer – Chetan Haritas Apr 22 '21 at 18:27
  • Yes, and at that point the `` is already a lie: it really contains strings, but due to raw types the compiler won't complain (except that you use a raw type: you **did** get a warning about that, didn't you? but you ignored it, cheekily). – Joachim Sauer Apr 22 '21 at 18:29
  • Is it a good practice to use generic instance. – Chetan Haritas Apr 22 '21 at 18:40
  • What do you mean by "generic instance"? If you mean `ArrayList` instead of `ArrayList`: yes, absolutely. Using the raw type (i.e. the one without ``) is only allowed for backwards compatibility with ancient code, actively breaks the type system and produces a warning whenever used (you did see the compiler warning, didn't you?). All of that is explained in the question/answer linked to above. – Joachim Sauer Apr 22 '21 at 18:43
  • WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'. It will be removed in version 5.0 of the Android Gradle plugin. For more information, see http://d.android.com/r/tools/update-dependency-configurations.html. – Chetan Haritas Apr 22 '21 at 18:52
  • WARNING: The specified Android SDK Build Tools version (27.0.3) is ignored, as it is below the minimum supported version (29.0.2) for Android Gradle Plugin 4.1.3. – Chetan Haritas Apr 22 '21 at 18:52
  • WARNING: Configuration 'testCompile' is obsolete and has been replaced with 'testImplementation'. It will be removed in version 5.0 of the Android Gradle plugin. For more information, see http://d.android.com/r/tools/update-dependency-configurations.html. – Chetan Haritas Apr 22 '21 at 18:53
  • Poor title. Rewrite to summarize your specific technical issue. – Basil Bourque Apr 22 '21 at 20:32

1 Answers1

1

It's actually about Java rather than Android. Java Collections (e.g. ArrayList) use collections of objects under the hood and when you define an ArrayList<Integer> and an ArrayList<String> there actually no difference between them. They both hold a Object[] inside. So actually the only difference they have, is how the compiler treat them and if we use generic functions like get it will lead to a ClassCastException because you can find a unchecked cast inside their implementation.

ArrayAdapter is also defined in the same way and actually Lists and ArrayAdapter are not checking any types. In other words you've fooled the IDE, lint and compiler and because there is not type checking here, it can work without any problem (I'm not sure but I think inside ArrayAdapter there is a code which calls toString for each element or checks if it's a string or not using instanceof). In addition, even if you use List<Object> it is still able to function without any problem.

Amin
  • 3,056
  • 4
  • 23
  • 34