-4

I am working on map v2. I wanted to convert latitude and longitude into the text address.So i used Geocoder class to locate the address. Now the problem is when i used List, it gives so many confusing locations. It might be the list of locations. I want to retrieve just the first location among them and pass that location to another activity. How can i do that? Please help.Its an emergency.Below is my code:

  try {
    Geocoder gcd = new Geocoder(this, Locale.getDefault());
      List<Address> addresses = gcd.getFromLocation( dmylats,  dmylong,  1);         

      if (addresses.size() > 0) 
          Toast.makeText(this, "You are at "+addresses,Toast.LENGTH_LONG).show();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

I just want the first value among the List.

3 Answers3

4

This can be archived via

addresses.get(addresses.size()-1)

Edit: The title said "last value" before OP's edit...

To answer the new question:

addresses.get(0)
smoove
  • 3,920
  • 3
  • 25
  • 31
1
addresses.get(addresses.size()-1)
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0
try {
            // Get the location manager
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                bestProvider = locationManager.getBestProvider(criteria, false);
                Location location = locationManager
                        .getLastKnownLocation(bestProvider);
                double lat = location.getLatitude();
                double lon = location.getLongitude();
                Geocoder gc = new Geocoder(this);
                List<Address> lstAdd = gc.getFromLocation(lat, lon, 1);
                Address ad = lstAdd.get(0);
                String str = ad.getAddressLine(0);
            TToast.makeText(this, "Your current location is " + str,
                    Toast.LENGTH_LONG).show();

} catch (Exception e) {}
Andy
  • 5,379
  • 7
  • 39
  • 53
  • it gives an error : 09-17 06:35:13.666: E/AndroidRuntime(1602): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 –  Sep 17 '14 at 10:35
  • Means that array list has no data.. Please check your code and manifest permissions. – Andy Sep 18 '14 at 06:00