+1  A: 

I think the preferred way is to update the adapter itself and not to replace it. Maybe you can write a method that merges the new and old data using the adapters insert() and remove() methods. I think that should keep your position.

Added Information:

I use the following as basic structure. Maybe it helps.


public class PlaylistAdapter extends ArrayAdapter<Playlist> {

    private ArrayList<Playlist> items;

    public PlaylistAdapter(Context context, int textViewResourceId, ArrayList<Playlist> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

}
hacksteak25
I tried to see if I could get this to work easily before trying to do the merging of data. However, this code:MyListAdapter adapter = (MyListAdapter)getListAdapter(); adapter.clear();for(Object object : objects){ adapter.add(object);}results in a java.lang.UnsupportedOperationException at java.util.AbstractList.addThis is strange since MyListAdapter extends ArrayAdapter, which should have the add method available.
twilbrand
"java.lang.UnsupportedOperationException at java.util.AbstractList.add": How do you initialize the Superclass? I had the same problem when I set the adapter data as array (eg. of type MyObject[]). Try to use a List (eg. of type ArrayList<MyObject>). Arrays can not be resized but the List can. That should solve your UnsupportedOperationException.
hacksteak25
I tried to change my implementation from using MyObject[] to ArrayList<MyObject>. This required a bit of refactoring since I'm using a custom ListAdapter that was extending ArrayAdapter<MyObject>. In the constructor, I used ArrayList<MyObject>.toArray() to satisfy the super constructor because I couldn't find a suitable replacement for ArrayAdapter. In either case, my app runs, but the data is not being displayed at all now. What code can I show that would be helpful?
twilbrand
Passing the toArray()-value to the super constructor will lead to the same problem. The array size is not changeable.
hacksteak25
I added some code to my answer. Maybe it helps. How does your class look?
hacksteak25
Thanks, I'm getting closer using your suggestions. I'm getting an IndexOutOfBounds exception when getView() is called after add(). I'm not sure why. I've added more code.
twilbrand
Hello, I tried to reproduce your Exception... and failed. It works for me (clear all items and add new ones). Even more... the list keeps the scroll position it had before the data was replaced. I think your `mObjects` is out of sync with the data. It is really the SAME object that you've passed to the super constructor? Try using `getItem(position)` instead of `mObjects.get(position)`. That should work.
hacksteak25
I was finally able to get this to work. Your suggestion was ultimately correct, I needed to use an ArrayList to back my adapter. The change I needed was to add a 'setData()' method to my adapter so I could generate an ArrayList from my returned JSON every time, then set that as the internal data of the adapter.
twilbrand