2

I have a number of custom objects that are significantly different from each other. These objects are stored in different lists and are to be displayed in different ListViews. Because of the design of these ListViews, I need to create my own custom ArrayAdapter. The thing is, the ListView design is supposed to be the same for all ListViews regardless of the objects in the list.

To accomplish this I created an ArrayAdapter for every type of object. E.g. for a custom object coA I made an ArrayAdapter coAAdapter, and for custom object coB I made an ArrayAdapter coBAdapter. This seems very unnecessary to me, since there are next to no difference between these adapters except what kind of ArrayAdapter the custom adapters extends and their constructors:

Adapter for class CoA:

public class CoAAdapter extends ArrayAdapter<CoA> {
     public CoAAdapter (Context context, int resource, List<CoA> objects) {...}
}

Adapter for class CoB:

public class CoBAdapter extends ArrayAdapter<CoB> {
     public CoBAdapter (Context context, int resource, List<CoB> objects) {...}
}

Is this how custom ArrayAdapters are supposed to be used, or is there a way to make this design a lot simpler, like having one custom ArrayAdapter for all custom objects?

I have thought about some different solutions, like creating a generic custom adapter or using inheritance by creating a common base class for my custom objects, but after some research none of these seems like a good approach.

6
  • Do all your ListViews use the same layout for your rows? Commented Dec 16, 2013 at 9:44
  • Make one interface that suit to all Your Objects and can draw all needed data. Then You can write adapter for that interface Commented Dec 16, 2013 at 9:45
  • @Apoorv Yes, more or less. I have a couple of layouts for my list items which differs only in the text color, but otherwise they are identical. Commented Dec 16, 2013 at 9:49
  • do what @Gooziec is saying Commented Dec 16, 2013 at 9:51
  • @Gooziec Just to clarify, I should create a common interface and let all of my other classes extend it? At least that's what I've tried to do, but it doesn't work: the array adapter does not accept the child classes. Commented Dec 16, 2013 at 10:20

3 Answers 3

3

You can define your adapter like this:

 public class MyArrayAdapter<T> extends ArrayAdapter<T> 
 {
      HashMap<T, Integer> mIdMap = new HashMap<T, Integer>();

      public MyArrayAdapter(Context context, int textViewResourceId, List<T> objects) {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.size(); ++i) {
          mIdMap.put(objects.get(i), i);
        }
      }
  }
Sign up to request clarification or add additional context in comments.

4 Comments

This approach crossed my mind, but I read that this is not an all to great solution due to not being type safe?
It is type-safe. Why do you think it is not?
Sorry, I read about it in some similar questions and I misinterpreted one of the answers. I'll try it then!
That's cool but there is more. Now my spinner is displaying a list of object references, will begin the search for the method to override......
0

Probably, you want to use a BaseAdapter. And in the getView() of that adapter, check for the instanceof of the object, do things accordingly.

Comments

0
public interace SomeInterface
{
    public String getString();
}

public class classA implements SomeInterface
{
    ...

    @Override
    public String getString()
    {
        return "blabla";
    }
}

public class classB implements SomeInterface
{
    ...

    @Override
    public String getString()
    {
        return "whatever";
    }
}

public class InterfaceAdapter extends ArrayAdapter<SomeInterface> 
{
     public InterfaceAdapter (Context context, int resource, List<SomeInterface> objects) {...}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.