Question

I am implementing two fragments to display different views on the same table retrieved from a ContentProvider: 1/ A class which extends ListFragment to provide the data in a list 2/ A custom fragment class which itself contains a Google Maps v2 fragment

The list fragment is a standard implementation using a loader and SimpleContentAdapter, so when the name of a list item is edited in a seperate activity, the change is reflected back in the list.

I want to do something similar with my custom fragment so that map markers are created and updated as the data changes.

What is the best practice for doing this?

It appears that using a SimpleCursorAdapter would not work since it returns View objects whereas I need to create/update Marker objects and these are not derived from View. I'm thinking along the lines of creating my own CursorAdapter class, implementing ContentObserver on my map fragment container and registering this with the CursorAdapter. Is this best practice? Does anyone know of any sample code demonstrating best practice?

Sorry if this is a stupid question - I'm new to Android programming so not completely clear on the notification architecture - but I have tried reading around the subject to no avail.

Was it helpful?

Solution

Okay, I've worked out the answer to this. There is no need for a CursorAdapter or a ContentObserver. The steps are as follows: 1/ Create Fragment-derived class (in my case derived from MapFragment) 2/ Implement LoaderCallbacks on this class 3/ Call getLoaderManager.initLoader() in onCreate() override of this class 4/ Create a new CursorLoader() in your LoaderCallbacks.onCreateLoader() override 5/ In LoaderCallbacks.onLoadFinished(), call code to iterate round the cursor and populate your view. But DON'T CLOSE THE CURSOR OTHERWISE YOU WON'T RECEIVE FURTHER UPDATES!!!

My problem arose from adapting some sample code in which the cursor.Close() was called once the list had been populated. I guess good practice is to save the cursor in a private data member and close it when LoaderCallbacks.onLoaderReset() is called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top