我有當前位置緯度&經度與我。我從Google地方API Web服務獲取用戶附近ATM的列表,並將我的數據存儲在Arraylist中,如下所示。如何在Android中對HashMap(ArrayList內)進行排序?
代碼段
double currentLat = xxx;
double currentLang = xxx;
按鈕點擊我叫谷歌地方的Web服務。
public void buttonClicked(View v) {
ParseXMLData xmlData = new ParseXMLData();
url = baseUrl + "location=" + latitude + "," + longitude + "&" + "radius=" + search_Area_Radius + "&" + "name=" + nameofplace + "&" + "sensor=true" + "&" + "key=" + API_KEY;
xmlData.execute("");
}
存儲內部ArrayList中的結果....
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("Name", XMLFunctions.getValue(e, "name"));
map.put("Vicinity", XMLFunctions.getValue(e, "vicinity"));
map.put("Latitude", XMLFunctions.getValue(e, "lat"));
map.put("Longitude", XMLFunctions.getValue(e, "lng"));
loc = new Location("");
loc.setLatitude(Double.parseDouble(XMLFunctions.getValue(e, "lat")));
loc.setLongitude(Double.parseDouble(XMLFunctions.getValue(e, "lng")));
獲得的B/W 2點之間的距離在這裏弗羅姆。
distance = currentLoc.distanceTo(loc);
map.put("Distance",String.valueOf(distance));
mylist.add(map);
loc = null;
distance = 0.0;
}
實現的比較和排序ArrayList的
public class PositionComparator implements Comparator<HashMap<String, String>> {
public PositionComparator() {
// TODO Auto-generated constructor stub
}
public int compare(HashMap<String, String> o1, HashMap<String, String> o2) {
/*System.out.println(o1.get("O1 Distance :- "));
System.out.println(o2.get("O2 Distance :- "));
System.out.println(o1.get("Distance").compareTo(o2.get("Distance")));
*/
return o1.get("Distance").compareTo(o2.get("Distance"));
}
}
現在發生什麼事是我有店內的數組列表進行排序,但問題是基於compareTo方法排序數據的數據僅限第一個字符。假設我的數據是2.12345和11.32545那麼它將取代爲2-1 = 1.
如何解決這個問題?
如果有人有任何建議/提示,請在此幫助我。
感謝
是不是太多的工作比較ArrayList中的結果,並將它們存儲在你想看到它們的順序數組?一旦你運行了你的查詢並得到了你的結果,你就知道該數組應該有多少個元素。 – 2012-02-26 09:18:45