Search code examples
androidlistviewgoogle-maps-api-2

horizontal listview in info window google map v2


I creating a application base on google map api v2

I want to put a horizontal list view contain image , which get from sd card, into info window there is my customwindow adapter class

class CustomInfoWindowAdapter implements InfoWindowAdapter {

    private final View mWindow;
    private final View mContents;
    LinearLayout myGallery;

    CustomInfoWindowAdapter() {
        mWindow = getLayoutInflater().inflate(R.layout.custom_info_window,
                null);
        mContents = getLayoutInflater().inflate(
                R.layout.custom_info_contents_with_listview, null);
    }

    @Override
    public View getInfoWindow(Marker marker) {

        render(marker, mWindow);
        return mWindow;
    }

    @Override
    public View getInfoContents(Marker marker) {

        render(marker, mContents);
        return mContents;
    }

    private void render(Marker marker, View view) {
        int badge = 0;

        myGallery = (LinearLayout) findViewById(R.id.mygallery);

        // get file from sd card
        String ExternalStorageDirectoryPath = Environment
                .getExternalStorageDirectory().getAbsolutePath();
        String targetPath = ExternalStorageDirectoryPath + "/DCIM/Camera";

        File targetDirector = new File(targetPath);

        File[] files = targetDirector.listFiles();

        if (marker.equals(listMarkers.get(0))) {
            for (File file : files) {
                       myGallery.addView(insertPhoto(file.getAbsolutePath()));
            }
        }

        String title = marker.getTitle();
        TextView titleUi = ((TextView) view.findViewById(R.id.title));
        if (title != null) {

            SpannableString titleText = new SpannableString(title);
            titleText.setSpan(new ForegroundColorSpan(Color.RED), 0,
                    titleText.length(), 0);
            titleUi.setText(titleText);
        } else {
            titleUi.setText("");
        }

        String snippet = marker.getSnippet();
        TextView snippetUi = ((TextView) view.findViewById(R.id.snippet));
        if (snippet != null && snippet.length() > 12) {
            SpannableString snippetText = new SpannableString(snippet);
            snippetText.setSpan(new ForegroundColorSpan(Color.MAGENTA), 0,
                    10, 0);
            snippetText.setSpan(new ForegroundColorSpan(Color.BLUE), 12,
                    snippet.length(), 0);
            snippetUi.setText(snippetText);
        } else {
            snippetUi.setText("");
        }
    }
}

when i run the code , locat show Null pointer exeption at

 for (File file : files) {
                            myGallery.addView(insertPhoto(file.getAbsolutePath()));

xml code

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

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <LinearLayout
        android:id="@+id/mygallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        />
</HorizontalScrollView>

thank in advance for any help .


Solution

  • Right now you have a NullPointerException but even if you will put all your images into this InfoWindow layout you will not be able to scroll it as the InfoWindow is not a real view. It's just a rendered SurfaceView. So you can't apply onTouch or onClick events on it.

    From Google Docs: https://developers.google.com/maps/documentation/android/marker#info_windows

    Note: The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (e.g., after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.