0

If I use ArrayAdapter or ArrayAdapter<String>, it does not affect my code.

For example, if I write it like:

ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, values);

Instead of:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, values);

Are these two things different? What is the purpose of <String> here?

I have searched the web and only knew that <String> shows something like collection?! Not sure.

I am a newbie in Android. Please help.

ChuongPham
  • 4,761
  • 8
  • 43
  • 53
krupal
  • 854
  • 2
  • 11
  • 17
  • @Code-Apprentice it isn't...please answer if you know – krupal Sep 14 '14 at 06:11
  • Yes it is. The `` indicates that the `ArrayAdapter` is a generic class. Everything in that Q&A about `List` applies equally well to `ArrayAdapter`. There are lots of other sources about generics on the Internet, so I suggest you do some more research. – Code-Apprentice Sep 14 '14 at 06:15
  • @Code-Apprentice so, if I write ArrayAdapter, it will always have to return string objects, but not in arrayadapter only? Also say me which is efficient or true approach? – krupal Sep 14 '14 at 06:17
  • This [link](http://www.oracle.com/technetwork/articles/javase/generics-136597.html) explains usage of `Java Generics` for you. – ChuongPham Sep 14 '14 at 06:18
  • 1
    Yes, that is true. However, the use of just `ArrayAdapter` is frowned upon as it circumvents the powerful type-checking of the compiler. If you want to store several different types of objects in an `ArrayAdapter`, there are better solutions, such as using an inheritance hierarchy. – Code-Apprentice Sep 14 '14 at 06:21
  • It sounds like you should just be learning how to program with Java only first. Throwing Android into the mix will only make it harder to learn how to program. – Ifrit Sep 14 '14 at 10:53

0 Answers0