Reputation: 22556
I find that when coding I make use of allot of ListVIews and most of my time is spent copy pasting and modifying the previous activities Adapter to work with the new activities data. I want to implement a generic list adapter class that I can use for all of my ListViews regardless of their views.
I have done some searching for ways to do this and I came across this tutorial: Tutorial
He implements a Genrick Adapter him self. I have tried to implement his approach but I have gotten compiler issues in eclipse. regarding the generic types *however thats not the issue at hand).
I also came across this library: android-collections-list-and-spinner-adapter It claims to also do what I am looking for. Another link about it: Article on the lib
So My questions is more so what have other people done / used in order to achieve code Re-usability with ListViews.
Has Any one used the android-collections-list-and-spinner-adapter
library before?
I am looking to do something along the lines of:
GenerickAdapter<MyObject> adapter = new GenerickAdapter<MyObject> (list<MyObject, context);`
listView.SetAdapter(adapter);
Upvotes: 0
Views: 968
Reputation: 6821
While the idea of passing a ViewFactory which handles view constructing is always an interesting idea, I'd stay clear of using that library. It's more then 3 years old and the author (at the time) seems not to understand adapters and performance.
First, it's a really bad idea to allow any collection type to be a backing for an adapter. It's critical for an adapter to be backed by something with fast random access times...Like an ArrayList
. An adapter's getView()
method can be invoked 3-4 times per position number during a rendering process. That's a lot of calls to get an item at a given position. The author of that library iterates over each collection every time a request for an item is made. That's insanely slow! It'll cause performance issues. That's why you don't see any kind of Set
adapter or LinkedList
adapter.
The closest thing I know of to that is the InstantAdapter found within the Adapter-Kit. It does have some restrictions but should work well for simple layouts. Here's a video from them on how to use it. Otherwise, most adapter libraries will require you to create a new adapter class for each type of view layout you wish to support. Hence why Goran Horia Mihail suggested extending ArrayAdapter. If you want an adapter specifically designed for that approach, you could try Advanced-Adapters.
Upvotes: 1