【发布时间】:2013-11-29 08:31:43
【问题描述】:
我正在编写一个 android 应用程序来显示附近的位置,我有两个活动;一种是在地图上显示位置,另一种是在 ListView 中列出它们。
在第一个活动中,我将每个地点的信息存储在一个hashMap中,这些信息包括:地名,地点经纬度,这是在HashMap中存储信息的代码:
// 清除所有现有标记 mGoogleMap.clear();
for(int i=0;i<list.size();i++){
// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();
// Getting a place from the places list
//HashMap<String, String>
hmPlace = list.get(i);
// Getting latitude of the place
double lat = Double.parseDouble(hmPlace.get("lat"));
// Getting longitude of the place
double lng = Double.parseDouble(hmPlace.get("lng"));
// Getting name
String name = hmPlace.get("place_name");
// listP[i]=hmPlace.get("place_name");
Log.d("places=",hmPlace.get("place_name"));
// Getting vicinity
String vicinity = hmPlace.get("vicinity");
LatLng latLng = new LatLng(lat, lng);
// Setting the position for the marker
markerOptions.position(latLng);
// Setting the title for the marker.
//This will be displayed on taping the marker
markerOptions.title(name + " : " + vicinity);
// Placing a marker on the touched position
Marker m = mGoogleMap.addMarker(markerOptions);
// Linking Marker id and place reference
mMarkerPlaceLink.put(m.getId(), hmPlace.get("reference"));
}
}
在这个活动中,我有一个按钮可以将我引导到第二个活动,该活动必须在 ListView 中列出附近的地方;这是按钮的代码:
public void list_airports(View v)
{
Intent intent;
switch (v.getId()) {
case R.id.list_items:
intent = new Intent(getApplicationContext(), List_airports.class);
intent.putExtra("com.example.dashboard_our.hmPlace",hmPlace);
startActivity(intent);
}
}
在第二个活动中我这样做:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_airports);
Bundle extras = getIntent().getExtras();
HashMap<String, String> places1=(HashMap<String, String>) extras.getSerializable("com.example.dashboard_our.hmPlace");
final ListView listview = (ListView) findViewById(R.id.list);
list = new ArrayList<String>();
for (int i = 0; i < places1.size(); ++i) {
list.addAll(places1.values());
}
}
但它只是多次打印第一名的信息,我该如何解决这个问题??
这是剩下的代码:
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(2000).alpha(0)
.withEndAction(new Runnable() {
@Override
public void run() {
list.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
});
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
// System.out.println(pairs.getKey() + " = " + pairs.getValue());
list.add(pairs.toString());
it.remove(); // avoids a ConcurrentModificationException
}
}
这是解析 Json 并返回位置的“后台执行”代码:
/** A class to parse the Google Places in JSON format */
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{
JSONObject jObject;
// Invoked by execute() method of this object
@Override
protected List<HashMap<String,String>> doInBackground(String... jsonData) {
// List<HashMap<String, String>>
places = null;
PlaceJSONParser placeJsonParser = new PlaceJSONParser();
try{
jObject = new JSONObject(jsonData[0]);
/** Getting the parsed data as a List construct */
places = placeJsonParser.parse(jObject);
}catch(Exception e){
Log.d("Exception",e.toString());
}
return places;
}
地点:
List<HashMap<String, String>> places;
【问题讨论】:
-
by "打印第一个位置的信息" 你的意思是在日志中打印还是在列表视图中显示?如果在列表视图中.. 然后发布您的适配器代码。如果在日志中打印,那么您序列化数据的方式有问题
-
您应该使用 list.get(i) 来获取 hashmap 值。
-
@AmulyaKhare 在列表视图中,我将编辑我的问题
-
@HAL9000 我怎么能使用 list.get(i)??我想将 HashMap 中的值添加到列表中,然后将它们放入 ListView
-
@AmulyaKhare 检查编辑