Add onSaveInstanceState()

This commit is contained in:
misaochan 2016-09-06 19:04:23 +12:00
parent 9a8bbb4dd1
commit 2b4f29b625
2 changed files with 19 additions and 14 deletions

View file

@ -30,6 +30,7 @@ public class NearbyListFragment extends ListFragment implements TaskListener {
private NearbyAsyncTask nearbyAsyncTask; private NearbyAsyncTask nearbyAsyncTask;
private NearbyAdapter mAdapter; private NearbyAdapter mAdapter;
private ListView listview;
private ProgressBar progressBar; private ProgressBar progressBar;
private boolean isTaskRunning = false; private boolean isTaskRunning = false;
@ -59,8 +60,18 @@ public class NearbyListFragment extends ListFragment implements TaskListener {
} }
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); listview = (ListView) getView().findViewById(R.id.listview);
if(savedInstanceState == null) {
mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation();
nearbyAsyncTask = new NearbyAsyncTask(this);
nearbyAsyncTask.execute();
progressBar.setVisibility(View.VISIBLE);
Log.d(TAG, "Saved instance state is null, populating ListView");
}
// If we are returning here from a screen orientation // If we are returning here from a screen orientation
// and the AsyncTask is still working, re-create and display the // and the AsyncTask is still working, re-create and display the
// progress dialog. // progress dialog.
@ -70,15 +81,8 @@ public class NearbyListFragment extends ListFragment implements TaskListener {
} }
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onSaveInstanceState(Bundle outInstanceState) {
outInstanceState.putInt("value", 1);
progressBar.setVisibility(View.VISIBLE);
mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation();
nearbyAsyncTask = new NearbyAsyncTask(this);
nearbyAsyncTask.execute();
Log.d(TAG, "Adapter set to ListView");
} }
@Override @Override
@ -137,7 +141,7 @@ public class NearbyListFragment extends ListFragment implements TaskListener {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
mAdapter = new NearbyAdapter(getActivity(), places); mAdapter = new NearbyAdapter(getActivity(), places);
ListView listview = (ListView) getView().findViewById(R.id.listview);
listview.setAdapter(mAdapter); listview.setAdapter(mAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@ -256,7 +260,6 @@ public class NearbyListFragment extends ListFragment implements TaskListener {
for(int i = 0; i < 500; i++) { for(int i = 0; i < 500; i++) {
Place place = places.get(i); Place place = places.get(i);
String distance = formatDistanceBetween(mLatestLocation, place.location); String distance = formatDistanceBetween(mLatestLocation, place.location);
System.out.println("Sorted " + place.name + " at " + distance + " away.");
place.setDistance(distance); place.setDistance(distance);
} }
return places; return places;

View file

@ -2,6 +2,7 @@ package fr.free.nrw.commons.nearby;
import android.net.Uri; import android.net.Uri;
import android.os.StrictMode; import android.os.StrictMode;
import android.util.Log;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@ -12,6 +13,7 @@ import java.util.List;
public class NearbyPlaces { public class NearbyPlaces {
private static final String TAG = "NearbyPlaces";
static List<Place> places = null; static List<Place> places = null;
public static List<Place> get() { public static List<Place> get() {
@ -30,6 +32,7 @@ public class NearbyPlaces {
boolean firstLine = true; boolean firstLine = true;
String line; String line;
Log.d(TAG, "Reading from CSV file...");
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
@ -39,7 +42,6 @@ public class NearbyPlaces {
continue; continue;
} }
System.out.println(line);
String[] fields = line.split(","); String[] fields = line.split(",");
String name = fields[0]; String name = fields[0];