Merge pull request #261 from misaochan/fix-double-listview-2

Fix doubled listview, and change method of handling screen config changes
This commit is contained in:
Josephine Lim 2016-09-06 19:24:25 +12:00 committed by GitHub
commit 166225c1ee
3 changed files with 106 additions and 69 deletions

View file

@ -1,6 +1,5 @@
package fr.free.nrw.commons.nearby; package fr.free.nrw.commons.nearby;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
@ -9,21 +8,16 @@ import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.ListFragment; import android.support.v4.app.ListFragment;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.Collections; import java.util.Collections;
@ -32,80 +26,103 @@ import java.util.List;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
public class NearbyListFragment extends ListFragment { public class NearbyListFragment extends ListFragment implements TaskListener {
private int mImageSize; private NearbyAsyncTask nearbyAsyncTask;
private boolean mItemClicked;
private NearbyAdapter mAdapter; private NearbyAdapter mAdapter;
private ListView listview;
private ProgressBar progressBar;
private boolean isTaskRunning = false;
private List<Place> places; private List<Place> places;
private LatLng mLatestLocation; private LatLng mLatestLocation;
private ProgressBar progressBar;
private static final String TAG = "NearbyListFragment"; private static final String TAG = "NearbyListFragment";
public NearbyListFragment() { public NearbyListFragment() {
} }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
Log.d(TAG, "NearbyListFragment created"); Log.d(TAG, "NearbyListFragment created");
View view = inflater.inflate(R.layout.fragment_nearby, container, false); View view = inflater.inflate(R.layout.fragment_nearby, container, false);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
return view; return view;
} }
@Override @Override
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
progressBar = (ProgressBar) view.findViewById(R.id.progressBar); //Check that this is the first time view is created, to avoid double list when screen orientation changed
progressBar.setMax(10); if(savedInstanceState == null) {
mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation();
listview = (ListView) getView().findViewById(R.id.listview);
nearbyAsyncTask = new NearbyAsyncTask(this);
nearbyAsyncTask.execute();
progressBar.setVisibility(View.VISIBLE);
Log.d(TAG, "Saved instance state is null, populating ListView");
} else {
progressBar.setVisibility(View.GONE);
}
// If we are returning here from a screen orientation and the AsyncTask is still working,
// re-create and display the progress dialog.
if (isTaskRunning) {
progressBar.setVisibility(View.VISIBLE);
}
}
@Override
public void onSaveInstanceState(Bundle outInstanceState) {
// See http://stackoverflow.com/questions/8942135/listview-added-dublicate-item-in-list-when-screen-orientation-changes
outInstanceState.putInt("value", 1);
}
@Override
public void onTaskStarted() {
isTaskRunning = true;
progressBar.setVisibility(View.VISIBLE); progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
mLatestLocation = ((NearbyActivity) getActivity()).getmLatestLocation();
getNearbyPlaces nearbyList = new getNearbyPlaces();
nearbyList.execute();
Log.d(TAG, "Adapter set to ListView");
} }
private List<Place> loadAttractionsFromLocation(final LatLng curLatLng) { @Override
public void onTaskFinished(List<Place> result) {
List<Place> places = NearbyPlaces.get(); if (progressBar != null) {
if (curLatLng != null) { progressBar.setVisibility(View.GONE);
Log.d(TAG, "Sorting places by distance...");
Collections.sort(places,
new Comparator<Place>() {
@Override
public int compare(Place lhs, Place rhs) {
double lhsDistance = computeDistanceBetween(
lhs.location, curLatLng);
double rhsDistance = computeDistanceBetween(
rhs.location, curLatLng);
return (int) (lhsDistance - rhsDistance);
}
}
);
} }
isTaskRunning = false;
for(int i = 0; i < 500; i++) {
Place place = places.get(i);
String distance = formatDistanceBetween(mLatestLocation, place.location);
System.out.println("Sorted " + place.name + " at " + distance + " away.");
place.setDistance(distance);
}
return places;
} }
private class getNearbyPlaces extends AsyncTask<Void, Integer, List<Place>> { @Override
public void onDetach() {
// All dialogs should be closed before leaving the activity in order to avoid
// the: Activity has leaked window com.android.internal.policy... exception
if (progressBar != null && progressBar.isShown()) {
progressBar.setVisibility(View.GONE);
}
super.onDetach();
}
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<Place>> {
private final TaskListener listener;
public NearbyAsyncTask (TaskListener listener) {
this.listener = listener;
}
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
super.onPreExecute(); super.onPreExecute();
lockScreenOrientation(); listener.onTaskStarted();
} }
@Override @Override
@ -124,16 +141,14 @@ public class NearbyListFragment extends ListFragment {
protected void onPostExecute(List<Place> result) { protected void onPostExecute(List<Place> result) {
super.onPostExecute(result); super.onPostExecute(result);
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
unlockScreenOrientation();
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() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Place place = places.get(position); Place place = places.get(position);
LatLng placeLatLng = place.location; LatLng placeLatLng = place.location;
@ -151,27 +166,11 @@ public class NearbyListFragment extends ListFragment {
} }
} }
}); });
listener.onTaskFinished(result);
mAdapter.notifyDataSetChanged(); mAdapter.notifyDataSetChanged();
} }
} }
private void lockScreenOrientation() {
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
private void unlockScreenOrientation() {
try {
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
} catch (NullPointerException e){
Log.e(TAG, "NPE: ", e);
}
}
private class NearbyAdapter extends ArrayAdapter<Place> { private class NearbyAdapter extends ArrayAdapter<Place> {
public List<Place> placesList; public List<Place> placesList;
@ -241,6 +240,33 @@ public class NearbyListFragment extends ListFragment {
} }
} }
private List<Place> loadAttractionsFromLocation(final LatLng curLatLng) {
List<Place> places = NearbyPlaces.get();
if (curLatLng != null) {
Log.d(TAG, "Sorting places by distance...");
Collections.sort(places,
new Comparator<Place>() {
@Override
public int compare(Place lhs, Place rhs) {
double lhsDistance = computeDistanceBetween(
lhs.location, curLatLng);
double rhsDistance = computeDistanceBetween(
rhs.location, curLatLng);
return (int) (lhsDistance - rhsDistance);
}
}
);
}
for(int i = 0; i < 500; i++) {
Place place = places.get(i);
String distance = formatDistanceBetween(mLatestLocation, place.location);
place.setDistance(distance);
}
return places;
}
private String formatDistanceBetween(LatLng point1, LatLng point2) { private String formatDistanceBetween(LatLng point1, LatLng point2) {
if (point1 == null || point2 == null) { if (point1 == null || point2 == null) {
return null; return null;

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];

View file

@ -0,0 +1,9 @@
package fr.free.nrw.commons.nearby;
import java.util.List;
public interface TaskListener {
void onTaskStarted();
void onTaskFinished(List<Place> result);
}