Search code examples
androidsingletonadaptersimpleadapter

How to make SimpleAdapter a Singleton in Android?


i realized that if you try to extend the SimpleAdapter class, you must make the constructor public -

public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
    super(context, data, resource, from, to);
}

but this will make it so that someone (or i) could accidentally create a new adapter, right?

i tried to make all of the data and methods static in the adapter, but it just doesn't seem like the right way to do things...? sorry, i don't mean to be too picky. i just don't want someone to accidentally instantiate an instance of MySimpleAdapter

How should i make this class a singleton?

the way i'm using SimpleAdapter is that i use it as a container to hold all the objects that i need in a dynamically shrinking/growing listview. At any point in the app, there should only be one such container. am i approaching this problem correctly?


Solution

  • You really don't want to make the adapter a static singleton. The reason for this is that it has a reference to the context. By making the adapter static, the context will never be garbage collected.

    If anything, you want to keep a static instance of your List. On each activity, simply pass in a reference to the shared list and off you go.