views:

104

answers:

4

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.

+2  A: 

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

}
st0le
But will the constantly declaring of the same variables e.g `point` and `overlayitem` not cause the one before to be overwritten?
Ulkmun
shouldnt do Meowmix, the variables are local to that loop iteration scope only.
Mauro
@Meowmix - no they won't. The `new` operation creates a new object each time. (@Mauro - scoping is not relevant. This would be the same if `point` and `overlayItem` were declared outside of the loop.)
Stephen C
@Meowmix: no, it won't. because in each iteration you are creating new object. just reference name is same as old one but object itself is different from old one.
mohammad shamsi
+1  A: 

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);
Stephen C
+1  A: 

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.

Carl Manaster
mcursor will probably have to call those methods in that order, so rearranging is not an option. Good intention, but alas. What Carl's answer does point out is that those fields don't need to be declared. So try calling `String tname = mcursor.getString(1)` for values that are used and calling `mcursor.getString(6)` (without the assignment) for values that are not used.
neXus
I'm sorry, @neXus, but that can't be right - the cursor is at a particular record position, and can get the *fields* of the record in any order. Until you call `moveToNext()`, nothing about the cursor or its access to data changes.
Carl Manaster
I must have confused it with something else then. Probably reading from a file with an InputStream. Never really used cursors but I thought I remembered. Thanks!
neXus
+2  A: 

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.

neXus