Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Benjamin Sam
10,215 PointsArrayAdapters for custom Objects
This question doesn't tie into any particular exercise within the Android classes, but meant more as a general question.
I'm trying to build an application that requires displaying custom objects in separate arraylists in to separate listviews. (ie. Dogs in a dog arraylist into a listview and cats in a cat arraylist in a listview, but not dogs and cats in the same arraylist or listview.) I understand there is a generic ArrayAdapter that displays strings in listview and to adapt custom objects into an ArrayAdapter, you have to create your own ArrayAdapter. But does this mean I have to create a separate ArrayAdapter for each and every custom object I want adapted into a listview even if they are handled the same way?
I hope that was clear. I appreciate any help I can get. Thanks!
1 Answer
Ernest Grzybowski
Treehouse Project ReviewerIf they are "handled" the same way... then the short answer is no. You do not need to make a custom adapter class for each custom object. You can make an AnimalArrayAdapter that can handle both Cat and Dog classes. Additionally, you could make Cat and Dog extend Animal. If you have any more questions or code you want me to look at, then make an edit to your post!
Benjamin Sam
10,215 PointsBenjamin Sam
10,215 PointsThey are handled exactly the same way, but similar. What I'm trying to do is create an adapter for a person object that will list each person's name in a listview and an adapter for an item that will list each item's name, quantity and price in another listview. So at a high level, the two are just adapting information into a listview.
Person Adapter
'''java public class PersonAdapter extends ArrayAdapter<Person>{
'''
Item Adapter '''java public class ItemAdapter extends ArrayAdapter<Item>{
'''
The structure is the same, but some of the internal code is different. Is there a way (or does it even make sense) to combine them into one general adapter and have it behave differently depending on the objects in the arraylist it is passed? If I keep them separate, it just feels like bad coding practice since I'm repeating a lot of the same stuff.
Thanks!
Ernest Grzybowski
Treehouse Project ReviewerErnest Grzybowski
Treehouse Project ReviewerIf you don't have a custom layout for your list items, you might be okay with using a regular adapter and using simpleListItemLayout.
"Is there a way (or does it even make sense) to combine them into one general adapter and have it behave differently depending on the objects in the arraylist it is passed?"You can definitely do that in your custom implementation of arrayAdapter. You can check to see what kind of data you or passing in... or you can even make another constructor that will take more or less parameters.