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?
views:
55answers:
1
+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
2010-07-28 04:23:27
If I use `ArrayAdaptor`, what do I do with the `textViewResourceId` given that I am using images rather than text?
Casebash
2010-07-28 04:42:57
@Casebash: Ignore it, override `getView()`, and return what you need.
CommonsWare
2010-07-28 05:26:55
But you have to pass something in. Would 0 work okay or would -1 be better?
Casebash
2010-07-28 05:36:55
@Casebash: I don't think it matters.
CommonsWare
2010-07-28 05:40:19
@Actually, that doesn't work. Both cause the program to crash
Casebash
2010-07-29 01:11:08
@Casebash: http://github.com/commonsguy/cw-android/tree/master/FancyLists/Recycling/
CommonsWare
2010-07-29 01:47:53
@CommonsWare: Wow, that excerpt is very nice! Thanks
Casebash
2010-07-29 02:38:26
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
2010-07-29 03:09:52
@Casebash: Ah, yes. For your case, you would not want to chain to the superclass.
CommonsWare
2010-07-29 03:25:48