2016-05-17 64 views
1

我有一個應用程序,從谷歌服務獲取定期的位置更新。這工作正常。 現在,我想每一個位置存儲位置的向量:如何創建位置矢量?

Vector vectorLocations = new Vector(); 

onLocationChanged我這樣做是爲了添加新的位置:

@Override 
public void onLocationChanged(Location location) { 

    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); 
    latitud.setText(String.valueOf(location.getLatitude())); 
    longitud.setText(String.valueOf(location.getLongitude())); 
    tiempo.setText(mLastUpdateTime); 
    velocidad.setText(String.valueOf(location.getSpeed())); 
    altura.setText(String.valueOf(location.getAltitude())); 
    vectorLocations.addElement(location); 
    ntextView.setText(String.valueOf(vectorLocations.lastIndexOf(location))); 

} 

但現在我想例如位置3,所以我做的:

Location positionthree = vectorLocations.elementAt(3); 

但我得到的錯誤不兼容的類型:

enter image description here

+0

¿爲什麼要使用非英文標點用英文寫的一個問題嗎? – quemeful

+1

可能是因爲他是一位西班牙語母語的人,因爲他的姓氏,來自阿根廷,他只是自動做到了。 –

+0

對不起,我來自西班牙。 –

回答

1

如果錯誤是一個Object是不是發現了一個Location的,你可以只投的對象的位置,像這樣:

Location positionthree = (Location) vectorLocations.elementAt(3); 

或者更簡單的辦法就是創造Location變量的ArrayList和訪問他們。你可以這樣做:

ArrayList<Location> allLocations = new ArrayList<>(); 
// On location change 
allLocations.add(location); 

// When you want to access them 
Location positionThree = allLocations.get(3); 

希望它有幫助!

+0

第二種解決方案比第一種更好嗎? –

+0

無論哪種解決方案適合您,都是最好的解決方案,如果它有效,我會與這個答案一起去做:) – AkashBhave

-1

最好使用ArrayList。

宣言:

ArrayList<Location> locations = new ArrayList<>();   

要添加元素:

locations.add(location); 

要在位置訪問元素:

Location positionThree = locations.get(3);   

但要記住該函數。獲得內部的索引3()實際上是指位置4作爲索引從0開始。

0

你需要轉換由elementAt()回到位置類型的對象像下面

Location positionthree = (Location) vectorLocations.elementAt(3); 
+0

2004年被調用,並且咕d了一些關於泛型的東西。 – njzk2

+0

這工作,謝謝。 –