I have a fragment with a listview in it, that retrieves the users location, then updates the list view based on the users location.
The problem with my code, is that it seems to grab the location update the list, then keep updating. And reloading the list, almost to the point of annoyance. Here is my code:
public class FindBrewery extends Fragment implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
LocationManager locationManager;
private String provider;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Context c = getActivity().getApplicationContext();
locationManager =(LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//todo change view
View rootView = inflater.inflate(R.layout.beer_location_list,container, false);
// Get the location manager
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
}
return rootView;
}
/* Request updates at startup */
@Override
public void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
public void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
//latituteField.setText(String.valueOf(lat));
//longitudeField.setText(String.valueOf(lng));
//Toast.makeText(this, "Finding your loaction",Toast.LENGTH_SHORT).show();
//call asycn task for location
String url = "myURL";
Log.d("urlTest", url);
//async task goes here
new GetNearbyBreweries(this.getActivity()).execute(url);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
//Toast.makeText(this, "Enabled new provider " + provider,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
//Toast.makeText(this, "Disabled provider " + provider,Toast.LENGTH_SHORT).show();
}
}