2012-05-28 101 views
0

你能告訴我請問如何將一個overlayItem對象添加到數組中? 我試過這樣:如何將一個overlayItem對象添加到數組中?

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; 

但我得到了一個錯誤:

Type mismatch: cannot convert from OverlayItem to List[]

+0

看到這個答案http://stackoverflow.com/a/10061636/1289716 – MAC

+0

什麼OverlayItem類型?通過這個類的代碼。 – dhams

+1

谷歌庫提供的OverlayItem:https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/OverlayItem – yoshi

回答

1

你混合數組和列表。如果你沒有使用數組,使用列表更容易,在這種情況下,你可能是指寫:

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

如果你想使用一個數組,你會寫這樣的 - 但你需要管理自己的數組的大小(這就是爲什麼使用列表如上更容易):

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

這是正確的,因爲看decleartion:OverlayItem overlayItemList<OverlayItem> arrItem[]
因此,如果您arrItem[] = overlayItem您隱含索賠List<OverlayItem> = OverlayItem。 :)
(我不知道,但我覺得它更「雪上加霜」與[]在變量名的結尾。我認爲括號是保留給陣列從而List<OverlayItem> arrItem[]導致一個陣列列表 OO)

您想要創建一個新的OverlayItem列表,然後將該項目添加到此列表中。我不知道,如果你可以實例List直接,所以我用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); 
相關問題