开发者

Java loop troubleshooting

I'm looking for the best way to do the following..

I've got a loop like so...

        while(mcursor.moveToNext()){
        String tname = mcursor.getString(4);
        String tmessage = mcursor.getStri开发者_如何学运维ng(7);
        String tlink = mcursor.getString(5);
        String tsname = mcursor.getString(3);
        Double tlat = mcursor.getDouble(1);
        Double tlng = mcursor.getDouble(2);
    }

for every element of the loop I want to apply the following...

     GeoPoint point = new GeoPoint(tlat,tlng);
   OverlayItem overlayitem = new OverlayItem(point, tname, tmessage);
    itemizedoverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedoverlay);

How would I do this? The answer is probably obvious but I'm confusing myself.


Something like this...

mcursor = getCursor(); //or whatever
if(mcursor != null && mcursor.moveToFirst())
{
        do
        {
        String tname = mcursor.getString(1);
        String tmessage = mcursor.getString(2);
        String tlink = mcursor.getString(3);
        String tsname = mcursor.getString(6);
        Double tlat = mcursor.getDouble(2);
        Double tlng = mcursor.getDouble(3);

        GeoPoint point = new GeoPoint(tlat,tlng);
        OverlayItem overlayitem = new OverlayItem(point, tname, tmessage);
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);


        }while(mcursor.moveToNext());

}


java uses references to values and that's where your problem lies.
As pointed out by @st0le you can just copy-paste your piece of code directly in the loop.

Why can we do that? Because calling GeoPoint point = new GeoPoint(tlat,tlng) will change the reference stored in point and not its value. The original value of point still exists and the reference to it was past on to the OverlayItem constructor.

while(mcursor.moveToNext()){
    String tname = mcursor.getString(4);
    String tmessage = mcursor.getString(7);
    String tlink = mcursor.getString(5);
    String tsname = mcursor.getString(3);
    Double tlat = mcursor.getDouble(1);
    Double tlng = mcursor.getDouble(2);

    GeoPoint point = new GeoPoint(tlat,tlng);
    OverlayItem overlayitem = new OverlayItem(point, tname, tmessage);
    itemizedoverlay.addOverlay(overlayitem);
}
mapOverlays.add(itemizedoverlay);

And you'll want that last line outside of the loop because otherwise the itemizedoverlay will be in mapOverlays multiple times. It is sufficient to add it just once. Either before entering the loop or after. Adding items to itemizedoverlay changes its value. Previous references to itemizedoverlay will still be the same and thus the changes will be immediately visible in mapOverlays.


Just some corrections to the code in the question...

Double tlat = mcursor.getDouble(2); // should this be 4?
Double tlng = mcursor.getDouble(3); // should this be 5?

// You probably don't want to do this for every loop iteration ...
mapOverlays.add(itemizedoverlay);


Reorganizing @st0le's answer, to group the data that's used together, together:

mcursor = getCursor(); //or whatever
if (mcursor != null && mcursor.moveToFirst()) {
    do {
        Double tlat = mcursor.getDouble(5);
        Double tlng = mcursor.getDouble(6);
        GeoPoint point = new GeoPoint(tlat,tlng);

        String tname = mcursor.getString(1);
        String tmessage = mcursor.getString(2);
        OverlayItem overlayitem = new OverlayItem(point, tname, tmessage);
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
        } while(mcursor.moveToNext());
    }

Which shows that we're not even using tlink & tsname, so I deleted them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜