0

This is a continuation of a research spawned by the answer to my older, broader question.

Is it correct to use a single Cursor (obtained via CursorLoader) to feed two distinct views, namely:

  1. ListView via SimpleCursorAdapter
  2. com.google.android.gms.maps.GoogleMap with Markers?

Results i'm having so far:

  1. If both views are given the cursor synchronously (i.e.: no other threads involved), than visually all works fine, except that UI is blocked considerably when populating GoogleMap
  2. If GoogleMap is being populated from Cursor in AsyncTask, then UI becomes responsive, but there're anomalies with the ListView: some rows are duplicated, also some markers are not getting to the map.

Details of my setup

  1. SQLite Cursor is loaded in FragmentActivity during onResume()
  2. The above activity hosts ViewPager with two fragments: ListFragment and SupportMapFragment (The consequence of using ViewPager is that both fragments will always be active)
  3. Cursor from activity is delivered to fragments via Otto bus (for unfamiliar: this is merely a thin replacement for callback interfaces, that delivers results synchronously on the main thread)
Community
  • 1
  • 1
esteewhy
  • 1,300
  • 13
  • 23

1 Answers1

1

I believe this is correct to do this. What could be improved though is that, if MapFragment is not visible it does not populate the Map. It populates it when user swipes to the MapFragment page.

Alternatively, you could even do following. When MapFragment is invisible it starts populating the map, but it does this in chunks (e.g. by 5 markers in a chunk). Once one chunk is populated, you can start with the next chunk by scheduling another runnable for been executed in main thread handler. This will not block main thread and will prepare the map "in background".

sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
  • 1
    Yes, i thought this too that populating map asynchronously would be the way to go. One particular problem with my setup is that the shared cursor must be somehow cached till some non-deterministic moment in the future when map might need it. Meanwhile, i've managed to ease the whole pain by switching from stock GoogleMap to android-maps-extensions, which, among all, provides async population. – esteewhy Aug 23 '13 at 09:34