1

I am trying to add small maps to a list and have created a Cursor adapter to handle a list of locations.

The layout for the individual location looks like this:

<LinearLayout
    ...  >

  <TextView android:id="@+id/start_loc"
     ...
   />

  <fragment android:id="@+id/start_loc_map"
            android:name="com.google.android.gms.maps.MapFragment"
     ...
   />

</LinearLayout>

Then in the adapter:

@Override
public void bindView(View view, Context context, Cursor cursor) {
  startLocView = (TextView) view.findViewById(R.id.start_loc);

then something like:

  SupportMapFragment startLocMap = 
       (SupportMapFragment) view.findViewById(R.id.start_loc_map);

or:

  SupportMapFragment startLocMap =
      (SupportMapFragment) getChildFragmentManager()
                             .findFragmentById(R.id.start_loc_map);

Neither of these works. The view (not surprisingly) can't be cast as a fragment and there is no fragment or activity available to the adapter. I can get a Context and (Activity) context.getFragmentManager but I can't find any way to get to getChildFragmentManager from that. I can get a FrameLayout, which is what the container object is but I don't know any way to reference to the Fragment contained therein.

How do I get a reference to a static (?) fragment in a layout from an adapter? Is getting a <fragment> less straightforward than a <TextView>?

albe
  • 551
  • 4
  • 15

1 Answers1

1

There would be a lot of work to display map in the list view using cursor adapter. The other problem that you shall be facing is the degraded performance and frequent app crash. Direct use of the mapView in a listView is a heavy operation. This is going to cause a delay and result will be sluggish User Experience of your app. To avoid this behavior you have an alternative to use Static maps. This will enable Lazy Loading of maps in your list view and therby user wont have to tap map now and then. If you are not providing current location of the user on the map litview Static maps are the best option.

I am providing a small example below that use this method. First create a list view from the data (Either API or DB). Then pass data such as latitude and longitude to a string with some static variables defined as follows.

String getMapURL = "http://maps.googleapis.com/maps/api/staticmap?zoom=12&size=360x180&markers=size:mid|color:green|"  
+ JOLocation.getString("latitude") 
+ "," 
+ JOLocation.getString("longitude") 
+ "&sensor=false";

The above constructed URL, when used in a browser, returns a .PNG file. Then, in my adapter for the activity, This method results in the lazy loading of the maps in the app where your marker displaying the coordinates are fetched at runtime, avoiding heavy work in the code.

Hope this would help!!!

AniV
  • 3,997
  • 1
  • 12
  • 17
  • Thanks for that. I was looking for a way to do something like that, however I would need to be able to mark up the map as you would through the maps API, adding markers. This would not necessarily need interaction at this stage. A poster in a similar thread (http://stackoverflow.com/questions/27397269/how-to-add-mapfragment-inside-a-listview-item-within-adapter) suggested using a "lite" (`options.liteMode(true);`) mode, and that would ameliorate the expected problems. I haven't tried that yet. – albe Jun 26 '15 at 13:41
  • In addition this does depend on location. – albe Jun 26 '15 at 13:42