Android 如何将动态数据从ArrayList设置到InfoWindow

Android 如何将动态数据从ArrayList设置到InfoWindow,android,arraylist,google-maps-android-api-2,infowindow,Android,Arraylist,Google Maps Android Api 2,Infowindow,我很难用动态内容填充InfoWindows。我有一个包含数据的ArrayList,我想填充infoWindow。如果在标记上使用方法片段,则动态显示数据,但全部显示在一行上,因此我被迫使用CustomInfoWindowAdapter。如果我使用CustomInfoWindowAdapter,我会为每个InfoWindow显示相同的数据。我错了什么 这是我的CustomInfoWindowAdapter: class MarkerInfoWindowAdapter implements Info

我很难用动态内容填充InfoWindows。我有一个包含数据的ArrayList,我想填充infoWindow。如果在标记上使用方法片段,则动态显示数据,但全部显示在一行上,因此我被迫使用CustomInfoWindowAdapter。如果我使用CustomInfoWindowAdapter,我会为每个InfoWindow显示相同的数据。我错了什么

这是我的CustomInfoWindowAdapter:

class MarkerInfoWindowAdapter implements InfoWindowAdapter {

        private View inflatedView;
        private View tempView;
        private String title, description;

        public void setTitle(String title) {
            this.title = title;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        MarkerInfoWindowAdapter() {
            inflatedView = getLayoutInflater().inflate(R.layout.info_window_layout, null);
         //   tempView = getLayoutInflater().inflate(R.layout.location_content_window, null);
        }

        @Override
        public View getInfoContents(Marker marker) {
          //  setInfo(marker, inflatedView);
            return inflatedView;
        }

        @Override
        public View getInfoWindow(Marker marker) {
           setInfo(marker, inflatedView);
            return inflatedView;
        }

        private void setInfo(Marker marker, View view) {
            final TextView txtTitle = (TextView) view.findViewById(R.id.title);
            final TextView txtDescription = (TextView) view.findViewById(R.id.lat_lng);
            txtTitle.setText(title);
            txtDescription.setText(description);
        }
    } 
这就是我从ArrayList创建标记和信息窗口的方法

public void drawRoute(HashMap<String, ArrayList<HashMap<String, String>>> result, String type) {
        ArrayList<HashMap<String, String>> voyageplan = result.get("optimal");
        HashMap<String, String> waypoint1;

        for (int i = 0; i < voyageplan.size(); i++) {

            waypoint1 = voyageplan.get(i);        

            googleMap.addMarker(new MarkerOptions().position(position)
             .title("WP# "+waypoint1.get("WP").toString()+" (optimal)")
             .snippet("prova")
             .anchor(0.5f, 0.5f).draggable(false)    
             .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

            infoWindowAdapter = new MarkerInfoWindowAdapter();
            infoWindowAdapter.setTitle(voyageplan.get(i).get("WP"));
            infoWindowAdapter.setDescription(voyageplan.get(i).get("lat"));
            googleMap.setInfoWindowAdapter(infoWindowAdapter);
    }
public void drawRoute(哈希映射结果,字符串类型){
ArrayList voyageplan=result.get(“最佳”);
HashMap航路点1;
对于(int i=0;i

提前感谢您的帮助

我不知道如何修复您的方法,但这就是我实现我的方法的方式。实际上,我有两种方法

第一种方法:创建另一个函数来获取数据 并在mapInitialization中调用该函数

第二种方法:保存每个marker.snippet中的所有动态数据 属性作为逗号分隔的值,并提取您的信息 通过处理分隔以逗号分隔的存储数据 对于每个标记

我实现了下面的第一个方法

// This function is where you read from your array, web, REST API or
// whatever to get the dynamic  data needed to put into your markers. 
//I just used a simple case of if and else to show dynamically changing
//string is returned. 

private String getDescriptionFromMarkerSoon(double latitude, double longitude) {
    // Access database or data structure here using arguments as key
    if (longitude > 0) {
        return "I am a boy";
    } else {
        return "I am a girl";
    }
}

// This is the one time setup that you need to do inside the setUpMapIfNeeded()
// function that is created by Android Studio if you initialize your project
// as a Google Maps Template. 
private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the referecen of the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            // add the layout for customizedInfoWindow in Google Maps
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

                @Override
                // Returning null here will automatically call getInfoContents
                public View getInfoWindow(Marker arg0) {
                    return null;
                }

                // Get the information to be displayed on the current window
                @Override
                public View getInfoContents(Marker marker) {
                    // Get the layout file for InfoContent
                    View v = getLayoutInflater().inflate(R.layout.info_window, null); // null means dont attach to any parent window
                    TextView tvDescription = (TextView) v.findViewById(R.id.tv_description);
                    TextView tvSnippet = (TextView) v.findViewById(R.id.tv_snippet);
                    TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
                    TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);

                    // Get position of marker
                    LatLng markerPos = marker.getPosition();
                    tvLat.setText("Latitude: " + markerPos.latitude);
                    tvLng.setText("Longitude: " + markerPos.longitude);
                    tvSnippet.setText(marker.getSnippet());
                   // Call the function you created here to get
                   // the dynamic data 
                   tvDescription.setText(getDescriptionFromMarkerSoon
                   (markerPos.latitude, markerPos.longitude));
                    // Return the view created
                    return v;
                }
            });
            setUpMap();

        }
    }
}
对于这个例子,我的layout/info_window.xml文件就是这样的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textStyle="bold"
        />


    <TextView android:id="@+id/tv_snippet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        />

    <TextView
        android:id="@+id/tv_lat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="5sp"

        />

    <TextView android:id="@+id/tv_lng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="5sp"

        />

    </LinearLayout>
</LinearLayout>