tags:

views:

55

answers:

1

I am going to make a grid of images and I am trying to figure out whether to use an array adaptor or a baseadaptor. While the GridView example, stores the data in an array, it uses a BaseAdapter rather than a ArrayAdaptor. I am curious why this is. One thing I noticed about an ArrayAdapter, is that its constructor takes a textViewResourceId for some unknown reason - although the documentation say the getView can be used to make it work with other kinds of views as well. So, if I want a fixed grid of images for a menu, which class would you recommend choosing?

+2  A: 

You typically choose your adapter class based on what the model data is. If you have an ArrayList of objects, use ArrayAdapter. If you have a Cursor from a database query, use a CursorAdapter. BaseAdapter can be used for anything, but it requires more coding, since it has no innate knowledge of how to iterate over the data.

CommonsWare
If I use `ArrayAdaptor`, what do I do with the `textViewResourceId` given that I am using images rather than text?
Casebash
@Casebash: Ignore it, override `getView()`, and return what you need.
CommonsWare
But you have to pass something in. Would 0 work okay or would -1 be better?
Casebash
@Casebash: I don't think it matters.
CommonsWare
@Actually, that doesn't work. Both cause the program to crash
Casebash
@Casebash: http://github.com/commonsguy/cw-android/tree/master/FancyLists/Recycling/
CommonsWare
@CommonsWare: Wow, that excerpt is very nice! Thanks
Casebash
BTW, you were right. It doesn't make a difference - as long as when you override `getView` you don't call the method in the super class
Casebash
@Casebash: Ah, yes. For your case, you would not want to chain to the superclass.
CommonsWare