Benaya Rio
Benaya Rio

Reputation: 125

using indexOf method to get a string from arraylist but it considered as integer

I have 3 arraylist of restaurant's name, langitude and longitude. I want to iterate for all the data on the list then create a google map marker with each of those data. But the problem is, everytime i tried to get the restaurant's name by using the indexof method on the restoName list (which is an arraylist of string), it always considered as an integer.

    List<String> RestoName;
    List<Double> RestoLang;
    List<Double> RestoLong;

    RestoName = createRestoNameList();
    RestoLang = createRestoLangList();
    RestoLong = createRestoLongList();

    for(int i = 0; i < restoList.size(); i++){

        String title = RestoName.indexOf(i);

        mMap.addMarker(new MarkerOptions().position(new LatLng(RestoLang.indexOf(i), RestoLong.indexOf(i)))
                .title(title));

    }

Upvotes: 0

Views: 84

Answers (2)

amirhesni
amirhesni

Reputation: 459

indexOf() method returns you position of item in that list for example if your second item of restaurant name is "Mac Donald" , RestoName.indexOf("Mac Donald") returns 2 i.e. the position of "Mac Donald" you should use get() method like this RestoName.get(i)

you can also make a Restaurant object which has name ,Latitude and Longitude and then make a list from restaurant object instead of make three list

List<Restaurant> restaurantList = new ArrayList();



for(int i = 0; i < restaurantList .size(); i++){

    String title = restaurantList.get(i).getName();

    mMap.addMarker(new MarkerOptions().position(new LatLng(restaurantList.get(i).getLat(), restaurantList.get(i).getLang()))
            .title(title));

}

Upvotes: 1

hleme
hleme

Reputation: 90

The indexOf () method looks for the first occurrence of an object within a list. As the list is String, the indexOf ("string") must be the search for a string within the list. This is not what you need. What you need is the get (int index) method.

String title = RestoName.get(i);
mMap.addMarker(new MarkerOptions().position(new LatLng(RestoLang.get(i), RestoLong.get(i)))
            .title(title));

Upvotes: 2

Related Questions