Andreea
Andreea

Reputation: 151

How can I add an overlayItem object to an array?

Can you tell me please how I can add an overlayItem object to an array? I tried in this way:

    GeoPoint point = new GeoPoint((int)(Double.parseDouble(arrCoordonate[1])),(int)(Double.parseDouble(arrCoordonate[0])));
    OverlayItem overlayItem = new OverlayItem(point, Double.parseDouble(arrCoordonate[1]) + "", Double.parseDouble(arrCoordonate[0]) +"");
    List<OverlayItem> arrItem[] = overlayItem;

But I got an error:

Type mismatch: cannot convert from OverlayItem to List[]

Upvotes: 0

Views: 557

Answers (2)

nuala
nuala

Reputation: 2689

This is right because look at the decleartion: OverlayItem overlayItem and List<OverlayItem> arrItem[].
So if you arrItem[] = overlayItem you implicit claim List<OverlayItem> = OverlayItem. :)
(I'm not sure but I think it's even "worse" with the [] at the end of variable's name. I think the brackets are reserved for arrays thus List<OverlayItem> arrItem[] results in an Array of Lists o.O)

You want to create a new List of OverlayItems and thenn add the item to this list. I'm not sure if you can instantiate List directly so I use ArrayList:

GeoPoint point = new GeoPoint((int)(Double.parseDouble(arrCoordonate[1])),(int)(Double.parseDouble(arrCoordonate[0])));
    OverlayItem overlayItem = new OverlayItem(point, Double.parseDouble(arrCoordonate[1]) + "", Double.parseDouble(arrCoordonate[0]) +"");
    ArrayList<OverlayItem> arrItem = new ArrayList<OverlayItem>();
    arrItem.add(overlayItem);

Upvotes: 0

assylias
assylias

Reputation: 328735

You are mixing arrays and lists. If you don't have to use an array, using a list is easier, in which case you probably meant to write:

List<OverlayItem> itemList = new ArrayList<OverlayItem> (); // create an empty list
itemList.add(overlayItem); // add you item to the list

If you want to use an array you would write it like this - but you need to manage the size of the array yourself (which is why using a list as above is easier):

OverlayItem[] itemArray = new OverlayItem[10]; //if you only need to insert 10 items
itemArray[0] = overlayItem;

Upvotes: 1

Related Questions