javaandroidosmdroid

osmdroid custom info window not accepting changes


I'm using a custom InfoWindow layout for my osmdroid markers(OSMDroid version 6.0.3)
Markers are added like this:

for(int i=0;i<10;i++){

GeoPoint startPoint = new GeoPoint(Double.parseDouble(lat_lon[0]), Double.parseDouble(lat_lon[1]));
                        Marker startMarker = new Marker(map);
                        startMarker.setPosition(startPoint);
                        InfoWindow infoWindow = new MyInfoWindow(R.layout.listview_layout, map,device);
                        startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);                            
                        startMarker.setInfoWindow(infoWindow);                            
                        startMarker.setId(Integer.toString(i));
                        startMarker.setOnMarkerClickListener(new Marker.OnMarkerClickListener() {
                            @Override
                            public boolean onMarkerClick(Marker marker, MapView mapView) {
                                int d_id=Integer.parseInt(marker.getId());


                                start_details(items.get(d_id));
                                return true;
                            }
                        });
                        map.getOverlays().add(startMarker);

                        startMarker.showInfoWindow();
}

and my custom InfoWindow is an inner class of my activity and is defined as:

private class MyInfoWindow extends InfoWindow{
        public int index=0;
        private datalogger de=null;
        public MyInfoWindow(int layoutResId, MapView mapView,datalogger dev) {
            super(layoutResId, mapView);
            de=dev;
        }
        public void onClose() {
        }

        public void onOpen(Object arg0) {
            LayoutInflater l=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
            LinearLayout myRoot = new LinearLayout(getBaseContext());
            View v=l.inflate(R.layout.listview_layout,myRoot,false);
            TextView moreinfo=v.findViewById(R.id.details_txt);
            TextView name=v.findViewById(R.id.name_txt);
            name.setText(de.name);
}

After running my app, InfoWindows show up but the elements inside them have their predefined values from the android studio designer. I also tried using findViewById() directly (instead of using layoutinflater) but that returns a null value.


Solution

  • Your onOpen method inflates new layout, sets value for a text view in that layout...and than finishes leaving the layout for the garbage collector to be removed. The layout is not attched to a view hiearchy (because myRoot is not part of the view hiearchy) so it's not displayed anywhere.

    InfoWindow actually inflates and creates its view hiearchy for you. You can access it through the protected field mView.

    So your onOpen method could be written like this:

    public void onOpen(Object arg0) {
            TextView moreinfo=mView.findViewById(R.id.details_txt);
            TextView name=mView.findViewById(R.id.name_txt);
            name.setText(de.name);
    }
    

    For more insight you could check out the source code of the InfoWindow class.