0

I am using a loop to add markers to a Google Map. The loop did work before I implemented the if-statements. Why wont it work now? I just dont understand why it wont work, it seems logical?

for(int i=1;i<statoilnumber;i++) {
    String XCord = sharedPreferences.getString("XCord"+i,"0");

    String YCord = sharedPreferences.getString("YCord"+i, "0");
    int Title = sharedPreferences.getInt("Title" + i, 0);
    String Info = sharedPreferences.getString("Info" + i, "0");
    String Icon = sharedPreferences.getString("Icon" + i, "4");
    String realTitle = Integer.toString(Title);
    // Drawing marker on the map

    if(Icon=="4") {
        googleMap.addMarker(new MarkerOptions()
                                            .position(new LatLng(Double.parseDouble(YCord), Double.parseDouble(XCord)))
                                            .title(realTitle + ". Statoil")
                                            .snippet(Info)
                                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.statoil_correct)));
    }
    if(Icon=="1") {
        googleMap.addMarker(new MarkerOptions()
                            .position(new LatLng(Double.parseDouble(YCord), Double.parseDouble(XCord)))
                            .title(realTitle + ". Statoil")
                            .snippet(Info)
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.approved_scale)));
    }
    if(Icon=="2") {
        googleMap.addMarker(new MarkerOptions()
                            .position(new LatLng(Double.parseDouble(YCord), Double.parseDouble(XCord)))
                            .title(realTitle + ". Statoil")
                            .snippet(Info)
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.clock_scale)));
    }
    if(Icon=="3") {
        googleMap.addMarker(new MarkerOptions()
                            .position(new LatLng(Double.parseDouble(YCord), Double.parseDouble(XCord)))
                            .title(realTitle + ". Statoil")
                            .snippet(Info)
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.death_scale)));
    }
}
rtruszk
  • 3,902
  • 13
  • 36
  • 53
  • 1
    Define doesn't work... does it throw exceptions? does it draw things multiple times? does it always draw the "Icon==4" option?. You should really be doing either a switch or a if/else (and in either case, include a default option if Icon doesn't match any of your values) – Foon Jun 15 '15 at 20:51
  • 1
    Icon is a String, use `(Icon.equals("4"))` and so on..... – Daniel Nugent Jun 15 '15 at 20:51
  • http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Daniel Nugent Jun 15 '15 at 20:52

1 Answers1

0

You cant compare String with == sysmbol

you need to use Icon.equels("second string")

== symbol compares Object ref. not actual data

Gaurav
  • 3,615
  • 2
  • 27
  • 50