自定义InfowindowAdapter以从内容提供商添加图像

时间:2014-02-16 00:04:26

标签: android database sqlite google-maps google-maps-markers

我在地图上显示有标记的地方应用程序,我已经定制了infowindow来显示创建的地点的内容,标题,描述和图片,这些数据是通过内容提供商访问的,我的问题一切都很顺利直到我从数据库中删除一条记录,该应用程序变得疯狂,或破坏或只是奇怪识别记录ID等 问题显然在infowindow代码定制中,因为要删除它都能正常工作,创建,删除,创建标记等等。但即使我已经尝试过没有给出错误。

地狱代码:

class MyInfoWindowAdapter implements InfoWindowAdapter {

    View v;

    MyInfoWindowAdapter() {
        v = getLayoutInflater().inflate(R.layout.custom_info, null);

    }

    @SuppressWarnings("deprecation")
    @Override
    public View getInfoWindow(final Marker marker) {

        MapaLugaresActivity.this.marker = marker;

        c.requery();

        TextView tvTitulo = (TextView) v.findViewById(R.id.tituloi);
        tvTitulo.setText(marker.getTitle());
        // TextView tvDescr = (TextView) v.findViewById(R.id.descripcioni);
        // tvDescr.setText(marker.getSnippet());
        ImageView imgi = (ImageView) v.findViewById(R.id.imgi);

        if (marker.getSnippet() != null && marker != null
                && c.getCount() > 0) {

            try {

                if (c.moveToFirst()) {

                    c.moveToPosition(Integer.valueOf(marker.getSnippet()) - 1);

                    String img = c.getString(c.getColumnIndex("foto"));

                    if (img != null) {

                        resizedBitmap = DecodeImagen
                                .decodeSampledBitmapFromFile(img, 100, 100);
                        imgi.setAdjustViewBounds(true);
                        imgi.setImageBitmap(resizedBitmap);

                    } else {

                        imgi.setAdjustViewBounds(true);
                        imgi.setImageResource(R.drawable.paisaje);

                    }

                }
            } catch (NumberFormatException e) {

                e.printStackTrace();
            }

        }
        // imgi.setImageResource(R.drawable.rdlv);

        Typeface fuente = Typeface.createFromAsset(getAssets(),
                "TravelingTypewriter.ttf");

        tvTitulo.setTypeface(fuente);
        // tvDescr.setTypeface(fuente);

        return v;

    }

    @Override
    public View getInfoContents(Marker marker) {
        if (MapaLugaresActivity.this.marker != null
                && MapaLugaresActivity.this.marker.isInfoWindowShown()) {
            MapaLugaresActivity.this.marker.hideInfoWindow();
            MapaLugaresActivity.this.marker.showInfoWindow();
        }

        return null;
    }

}

添加标记:

                 @SuppressWarnings("deprecation")
public void addMarker() {

    map.clear();
    c.requery();

    if (c.getCount() > 0) {

        c.moveToFirst();

        do {

            latlong = new LatLng(c.getFloat(c.getColumnIndex("latitud")),
                    c.getFloat(c.getColumnIndex("longitud")));
            final MarkerOptions marker = new MarkerOptions().title(c
                    .getString(c.getColumnIndex("titulo")));
            marker.snippet(c.getString(c.getColumnIndex("_id")));

            marker.position(latlong);

            map.addMarker(marker).showInfoWindow();

        } while (c.moveToNext());

    } else {
        Toast.makeText(MapaLugaresActivity.this, "Añada un nuevo lugar",
                Toast.LENGTH_SHORT).show();
    }

}

我提前感谢您的帮助,花了几天时间试图找到解决方案,我认为是时候寻求帮助了,谢谢。我的编程经验有限,希望您的理解

PS:我已经在GitHub上传了这个项目,以防有人想分析它:D

活动MapaLugaresActivity中的代码

https://github.com/Shochlves/rdlv

图片信息:

https://lh5.ggpht.com/eBgrw0riQxfGph4FhxXvMRDxzYjlvdWw6L6qRqg_L1oV9yPGHPW9wV2Cal7zVGj1-HY=h900-rw

1 个答案:

答案 0 :(得分:1)

解决!该解决方案已实现Hastable(http://developer.android.com/reference/java/util/Hashtable.html),我点击了代码,因为它很有帮助。 InfoWindowAdapter,带有来自SQLite(内容提供商)的图像和标题

InfoWindowAdapter:

public class MyInfoWindowAdapter implements InfoWindowAdapter {



    private View v;

    MyInfoWindowAdapter() {
        v = getLayoutInflater().inflate(R.layout.custom_info, null);

    }
    @Override
    public View getInfoContents(Marker marker) {
        if (MapaLugaresActivity.this.marker != null
                && MapaLugaresActivity.this.marker.isInfoWindowShown()) {
            MapaLugaresActivity.this.marker.hideInfoWindow();
            MapaLugaresActivity.this.marker.showInfoWindow();
        }

        return null;
    }

    @Override
    public View getInfoWindow(Marker marker) {

        MapaLugaresActivity.this.marker = marker;
        String img = null;

         if (marker.getId() != null && markers != null && markers.size() > 0) {
                if ( markers.get(marker.getId()) != null &&
                        markers.get(marker.getId()) != null) {
                    img = markers.get(marker.getId());
                }
            }
        final ImageView imgi = (ImageView) v.findViewById(R.id.imgi);

        if (img != null && !img.equalsIgnoreCase("null")
                && !img.equalsIgnoreCase("")) {

            Bitmap resizedBitmap = DecodeImagen
                    .decodeSampledBitmapFromFile(img, 100, 100);

            imgi.setImageBitmap(resizedBitmap);

        } else {
            imgi.setAdjustViewBounds(true);
            imgi.setImageResource(R.drawable.paisaje);
        }


        TextView tvTitulo = (TextView) v.findViewById(R.id.tituloi);
        tvTitulo.setText(marker.getTitle());
        //TextView tvDescr = (TextView) v.findViewById(R.id.descripcioni);
        //tvDescr.setText(marker.getSnippet());



        Typeface fuente = Typeface.createFromAsset(getAssets(),
                "TravelingTypewriter.ttf");

        tvTitulo.setTypeface(fuente);
        // tvDescr.setTypeface(fuente);


        return v;

    }



}

addMarker:

public void addMarker() {

    markers = new Hashtable<String, String>();
    c.requery();

    if (c.getCount() > 0) {

        c.moveToFirst();

        do {


            latlong = new LatLng(c.getFloat(c.getColumnIndex("latitud")),
                    c.getFloat(c.getColumnIndex("longitud")));
            final Marker marker = map.addMarker(new MarkerOptions().title(c
                    .getString(c.getColumnIndex("titulo"))).snippet(c.getString(c.getColumnIndex("_id"))).position(latlong)); 

            markers.put(marker.getId(), c.getString(c.getColumnIndex("foto")));



        } while (c.moveToNext());

    } else {
        Toast.makeText(MapaLugaresActivity.this, "Añada un nuevo lugar",
                Toast.LENGTH_SHORT).show();
    }

}

的onCreate:

if (map != null) {
        map.setInfoWindowAdapter(new MyInfoWindowAdapter());

        addMarker();
    }

问候语