Reputation: 36404
I use this code to load the nearby places of any particular location by setting the** data using an Array Adapter. There are two issues that I face: 1. The spinner doesn't stop even after the data is loaded. (There should be no spinner. But I am not sure how does Fragment when it comes to this.)
I'm going to implement AsyncTask to get the places so that it doesn't slow down the Activity. The mini problem I face is this: How do I notify the user (update the view) with new date when he/she has changed his location. Let's assume that that the user is walking. Thus the lat/lon will change. So, how can I use onChangeNotify() and change the value of the List.
public class FragmentMap extends 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){
}
}
Upvotes: 1
Views: 956
Reputation: 2573
There are a few things in your code that looks a bit strange to me.
You never call setListAdapter()
which binds an adapter to the internal ListView of the ListFragment. Instead, you are fetching another ListView and set an adapter to that one. That is probably why no data gets displayed in the list, and you will instead se a progress indicator.
I'm not sure I understand your second question, but you should put your UI updates in the onPostExecute method of AsychTask.
Upvotes: 2