Basic AsyncTask code

This commit is contained in:
misaochan 2016-08-30 18:56:36 +12:00
parent 0733ea1a34
commit 2f4036c4a7
2 changed files with 35 additions and 8 deletions

View file

@ -2,6 +2,7 @@ package fr.free.nrw.commons.nearby;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v7.widget.RecyclerView;
@ -72,13 +73,9 @@ public class NearbyListFragment extends ListFragment {
mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation();
//Hardcoding mLatestLocation to Michigan for testing
//mLatestLocation = new LatLng(44.182205, -84.506836);
places = loadAttractionsFromLocation(mLatestLocation);
mAdapter = new NearbyAdapter(getActivity(), places);
ListView listview = (ListView) view.findViewById(R.id.listview);
//setListAdapter(mAdapter);
listview.setAdapter(mAdapter);
getNearbyPlaces nearbyList = new getNearbyPlaces();
nearbyList.execute();
Log.d(TAG, "Adapter set to ListView");
mAdapter.notifyDataSetChanged();
@ -113,6 +110,36 @@ public class NearbyListFragment extends ListFragment {
return places;
}
private class getNearbyPlaces extends AsyncTask<Void, String, List<Place>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
@Override
protected List<Place> doInBackground(Void... params) {
places = loadAttractionsFromLocation(mLatestLocation);
return places;
}
@Override
protected void onPostExecute(List<Place> result) {
super.onPostExecute(result);
mAdapter = new NearbyAdapter(getActivity(), places);
ListView listview = (ListView) getView().findViewById(R.id.listview);
//setListAdapter(mAdapter);
listview.setAdapter(mAdapter);
}
}
private class NearbyAdapter extends ArrayAdapter<Place> {
public List<Place> placesList;

View file

@ -14,7 +14,7 @@ public class NearbyPlaces {
static List<Place> places = null;
public static synchronized List<Place> get() {
public static List<Place> get() {
if(places != null) {
return places;
}