我正在尝试使用Google地图v2构建应用。我的目标是尝试获取用户当前位置,然后通过spinner1显示它们附近的位置。如果用户选择例如Hospital,那么在地图上它将向医院显示标记。以下是logcat的结果。
所以,我在spinner1的右侧添加了另一个微调器(spinner2)来存储所有这些医院名称。正如你所看到的,在logcat上有20家医院,但我的问题出在spinner2上,它只显示了1家医院。
如何将这20个数据添加到spinner2?这是我的代码:
...
@Override
protected void onPostExecute(List<HashMap<String, String>> list){
gmapv2.clear();
for(int i = 0; i<list.size(); i++){
MarkerOptions m_opt = new MarkerOptions();
HashMap<String, String> hash_tempat = list.get(i);
double llat = Double.parseDouble(hash_tempat.get("lat"));
double llong = Double.parseDouble(hash_tempat.get("lng"));
String nama = hash_tempat.get("place_name");
//Log.d("dari gg ke hendy", nama);
String vici = hash_tempat.get("vicinity");
Log.d("dari gg ke hendy", nama + " " + vici);
LatLng ab_latLng = new LatLng(llat, llong);
m_opt.position(ab_latLng);
m_opt.title(nama + " : " + vici);
m_opt.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
Marker e = gmapv2.addMarker(m_opt);
detailTempat.put(e.getId(), hash_tempat.get("reference"));
adapt = new ArrayAdapter<String>(Places_On_Resta_Map.this, android.R.layout.simple_spinner_item, new ArrayList<String>());
adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDetail.setAdapter(adapt);
adapt.add(nama);
pDialog.dismiss();
}
}
任何想法或帮助将不胜感激。 环境:Windows 7,Eclipse,Genymotion。
答案 0 :(得分:1)
试试如下:
ArrayList<String> myArray;
for(int i = 0; i<list.size(); i++){
MarkerOptions m_opt = new MarkerOptions();
HashMap<String, String> hash_tempat = list.get(i);
double llat = Double.parseDouble(hash_tempat.get("lat"));
double llong = Double.parseDouble(hash_tempat.get("lng"));
String nama = hash_tempat.get("place_name");
myArray.add(nama);
String vici = hash_tempat.get("vicinity");
Log.d("dari gg ke hendy", nama + " " + vici);
LatLng ab_latLng = new LatLng(llat, llong);
m_opt.position(ab_latLng);
m_opt.title(nama + " : " + vici);
m_opt.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
Marker e = gmapv2.addMarker(m_opt);
detailTempat.put(e.getId(), hash_tempat.get("reference"));
}
adapt = new ArrayAdapter<String>(Places_On_Resta_Map.this,
android.R.layout.simple_spinner_item, myArray); //Change your array here
adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDetail.setAdapter(adapt);
pDialog.dismiss();
答案 1 :(得分:1)
你可以这样做:
@Override
protected void onPostExecute(List<HashMap<String, String>> list){
gmapv2.clear();
adapt = new ArrayAdapter<String>(Places_On_Resta_Map.this, android.R.layout.simple_spinner_item, new ArrayList<String>());
adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for(int i = 0; i<list.size(); i++){
MarkerOptions m_opt = new MarkerOptions();
HashMap<String, String> hash_tempat = list.get(i);
double llat = Double.parseDouble(hash_tempat.get("lat"));
double llong = Double.parseDouble(hash_tempat.get("lng"));
String nama = hash_tempat.get("place_name");
//Log.d("dari gg ke hendy", nama);
String vici = hash_tempat.get("vicinity");
Log.d("dari gg ke hendy", nama + " " + vici);
LatLng ab_latLng = new LatLng(llat, llong);
m_opt.position(ab_latLng);
m_opt.title(nama + " : " + vici);
m_opt.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
Marker e = gmapv2.addMarker(m_opt);
detailTempat.put(e.getId(), hash_tempat.get("reference"));
adapt.add(nama);//<---------
pDialog.dismiss();
}
spinnerDetail.setAdapter(adapt);//<---------
}