Skizit
Skizit

Reputation: 44852

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.getString(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.

Upvotes: 0

Views: 244

Answers (4)

neXus
neXus

Reputation: 2223

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.

Upvotes: 2

Carl Manaster
Carl Manaster

Reputation: 40336

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.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718886

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);

Upvotes: 1

st0le
st0le

Reputation: 33545

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());

}

Upvotes: 2

Related Questions