我使用此代码通过使用数组适配器设置**数据来加载任何特定位置的附近地点。我面临两个问题:1.即使加载数据后微调器也不会停止。(应该没有微调器。但我不确定 Fragment 是如何做到这一点的。)
我将实施 AsyncTask 来获取这些位置,这样它就不会减慢 Activity 的速度。我面临的小问题是:当他/她更改位置时,我如何通知用户(更新视图)新日期。让我们假设用户正在走路。因此纬度/经度会改变。那么,如何使用 onChangeNotify() 并更改列表的值。
公共类 FragmentMap 扩展 ListFragment {
ArrayList<Place> places = new ArrayList<Place>(); //List<String> val = new List<String>() //@InjectView(R.id.explorePlacesListView) ListView placesListView; ListView listView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ListView listView = (ListView) getActivity().findViewById(R.id.explorePlacesListView); getPlaces(); PlacesAdapter placesAdapter = new PlacesAdapter(getActivity(),R.layout.user_list_item, places); listView.setAdapter(placesAdapter); } public void getPlaces(){ URL yahoo; String key,latitude,longitude; key = "AIzaSyAZgD01sj3jssaYCmkLL8c7Z4qPTEdt6xU"; latitude = "37.77264"; longitude ="-122.409915"; try { yahoo = new URL("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&sensor=false&key=AIzaSyB2SGEmOLDGB_f0bp1PGTjQqTw2VuDcaM8"); URLConnection yc = yahoo.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream())); String inputLine; String jsonLine = new String(); while ((inputLine = in.readLine()) != null) { Log.d("inputLine",inputLine); jsonLine = jsonLine + inputLine; } in.close(); Object jsonResponse = parseResponse(jsonLine); parseJSONPlaces((JSONObject)jsonResponse); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected Object parseResponse(String responseBody) throws JSONException { return new JSONTokener(responseBody).nextValue(); } private void parseJSONPlaces(JSONObject json){ try { JSONArray jsonArray = json.getJSONArray("results"); for (int j = 0; j < json.length(); j++) { Place place = new Place(); place.name = jsonArray.getJSONObject(j).getString("name"); place.icon = jsonArray.getJSONObject(j).getString("icon"); // JSONObject locationObject = jsonArray.getJSONObject(j).getJSONObject("location"); // place.latitude = locationObject.getString("latitude"); // place.longitude = locationObject.getString("longitude"); Log.d("name",place.name); places.add(place); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(json.optJSONObject("results") != null){ } }