0

Hi I'm trying to place customized listview on top of the mapFragment (I'm using GoogleMaps api V2). This mapFragment is within a Fragment of a ActionBarTab. I've looked into tileOverlay and groundOverlay, but they are all about placing images(as per my understanding) on the map. Is there a way to inflate a ListView on the mapFragment. This is my fragment layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/fragment_regional_analysis" >


        <ListView
            android:id="@android:id/list"
            android:layout_width="300dp"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:cacheColorHint="@android:color/transparent" />
        <View 
            android:id="@+id/mapView"                   
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

</LinearLayout>

This is fragment class

`

public class RegionalFragment extends Fragment 
  {

static final LatLng UK = new LatLng(53.4167, 3.0000);
static final LatLng LONDON = new LatLng(51.5171, 0.1062);
static final LatLng Brimingham = new LatLng(52.4800, 1.9100);
private MapFragment mapFragment;
private GoogleMap map;
ProgressBar progressBar;
int progress = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_container,
            container, false);
    mapFragment = new MapFragment() {
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            map = mapFragment.getMap();

            if (map != null) {
                 setupMap();
            }
        }
    };
    // fragment added dynamically
    FragmentTransaction transaction = getFragmentManager()
            .beginTransaction();
    transaction.add(R.id.mapView, mapFragment).commit();
    return v;
}`

Logcat errors:

04-16 09:38:40.300: E/AndroidRuntime(807): FATAL EXCEPTION: main
04-16 09:38:40.300: E/AndroidRuntime(807): java.lang.ClassCastException: android.view.View cannot be        cast to android.view.ViewGroup
04-16 09:38:40.300: E/AndroidRuntime(807):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:875)
04-16 09:38:40.300: E/AndroidRuntime(807):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
04-16 09:38:40.300: E/AndroidRuntime(807):  at android.app.BackStackRecord.run(BackStackRecord.java:682)
04-16 09:38:40.300: E/AndroidRuntime(807):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
04-16 09:38:40.300: E/AndroidRuntime(807):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
04-16 09:38:40.300: E/AndroidRuntime(807):  at android.os.Handler.handleCallback(Handler.java:725)
04-16 09:38:40.300: E/AndroidRuntime(807):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 09:38:40.300: E/AndroidRuntime(807):  at android.os.Looper.loop(Looper.java:137)
04-16 09:38:40.300: E/AndroidRuntime(807):  at android.app.ActivityThread.main(ActivityThread.java:5041)
04-16 09:38:40.300: E/AndroidRuntime(807):  at java.lang.reflect.Method.invokeNative(Native Method)
04-16 09:38:40.300: E/AndroidRuntime(807):  at java.lang.reflect.Method.invoke(Method.java:511)
04-16 09:38:40.300: E/AndroidRuntime(807):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 09:38:40.300: E/AndroidRuntime(807):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 09:38:40.300: E/AndroidRuntime(807):  at dalvik.system.NativeStart.main(Native Method)

Thanks in advance.

Amritha Dilip
  • 723
  • 1
  • 8
  • 16

1 Answers1

0

Can't you just put it into your layout file?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="AS_YOU_WANT_IT" />

<fragment
    android:id="@+id/mapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

And inflate this in your fragment?

Analizer
  • 1,594
  • 16
  • 30
  • sorry forgot to mention that my mapfragment is within a actionbar tab fragment. I tried putting listview there but my app crashed. – Amritha Dilip Apr 16 '13 at 06:38
  • could you post that solution's code and logcat result? or do you know the reason of the crash? – Analizer Apr 16 '13 at 06:45
  • There's no such errors in logcat, but the emulator screen starts blinking rapidly. Dont know why. But isnt there a way to inflate listview on the map?? – Amritha Dilip Apr 16 '13 at 07:36
  • I don't understand "on the map". It should overlap the map? In that case the solution would be puting listview and mapfragment into a FrameLayout instead of a LinearLayout – Analizer Apr 16 '13 at 07:41
  • "on the map" as in overlap the map, such that the map is visible through the listview. i'm doing nested fragments here, i cant put fragment into the framelayout. – Amritha Dilip Apr 16 '13 at 07:48
  • Well, I would put mapfragment and listview into a framelaout, and set the listview transparent. – Analizer Apr 16 '13 at 08:01
  • I get an runtime error if i do that ` 04-16 09:11:52.002: E/AndroidRuntime(823): FATAL EXCEPTION: main 04-16 09:11:52.002: E/AndroidRuntime(823): java.lang.ClassCastException: android.view.View cannot be cast to android.view.ViewGroup 04-16 09:11:52.002: E/AndroidRuntime(823): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:875)` – Amritha Dilip Apr 16 '13 at 09:15
  • could you please post your xml and your code? (and please indicate, which line is the one throwing the exception) – Analizer Apr 16 '13 at 09:18
  • If you want to use mapFragment (or supportMapFragment), modify your xml: from to And you can get your map like mapFragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapFragment)).getMap(); instead of new MapFragment() – Analizer Apr 16 '13 at 12:53
  • Nested fragments cant be done like that. Please read http://developer.android.com/about/versions/android-4.2.html#NestedFragments – Amritha Dilip Apr 17 '13 at 07:53
  • All right, but the reason of the crash is still that in the xml there is as the mapFragment instead of , this is what's causing the classCastException. For adding a List to the mapFragment you don't need two fragments, simply Put a ListView in the same layout as MapFragment is in. – Analizer Apr 17 '13 at 08:25