Skip to content Skip to sidebar Skip to footer

Android Getmaxaddresslineindex Returns 0 For Line 1

For some reason the implementation of getMaxAddressLineIndex has recently changed. Now this method returns 0 for line 1. I have an existing code, which used to work: i

Solution 1:

I had the same issue and this just a workaround.

if (address.getMaxAddressLineIndex() > 0) {
    for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
        addressFragments.add(address.getAddressLine(i));
    }
} else {
    try {
        addressFragments.add(address.getAddressLine(0));
    } catch (Exception ignored) {}
}

Hope this help

Solution 2:

The same happened to me. I verified that Developer Reference (https://developer.android.com/reference/android/location/Address.html#getMaxAddressLineIndex()) now states:

int getMaxAddressLineIndex ()

Returns the largest index currently in use to specify an address line. If no address lines are specified, -1 is returned.

So it seems to reflect the new behaviour.

Anyway, to be safe, I'm going to enclose getAddressLine(i) inside try\catch

Solution 3:

I think getAddressLine() has changed. It used to return the various elements of the address in separate calls to ...getAddressLine(0), ...getAddressLine(1) and so on up to getAddressLine(n) where n is ...getMaxAddressLineIndex().

Now it seems to return the entire address concatenated and comma-separated into the call to ...getAddressLine(0), and ...getMaxAddressLineIndex() always returns zero (if an address is returned, or -1 if no address returned).

For example, in the old version, the lat/long of the Houses of Parliament in London would return (for the first address returned) 4 address lines:

addressLines=[0:"9748 Abingdon Street",1:"Westminster, London",2:"SW1P 3JY",3:"UK"]

Now it returns one line:

addressLines=[0:"9748 Abingdon St, Westminster, London SW1P 3JY, UK"]

That seems to be what happens to me. I've tried it on a Moto G5 Plus with Android 7 and a Samsung tablet with Android 6. My Android emulators still work the old way. Someone tell me if I've got that wrong!

Note: in the past you could test i < address.getMaxAddressLineIndex() rather than <= . This just meant you didn't get the last element, which always seemed to be the abreviated country name (e.g. "USA"). Now that .getMaxAddressLineIndex() always seems to return zero, that won't work. But note you get "USA" appended to the string returned to .getAddressLine(0).

Solution 4:

As user8533943 has pointed out above the implementation has been changed surreptitiously. However I am of the opinion that

geocoder.getFromLocation()

has changed it's implementation. It returns a list of addresses and each address consists of address lines. But as far as I could see when I compiled with version 26 of the SDK there aren't "address lines" being returned but just one line for every address.

So if you have just one address then

getMaxAddressLineIndex()

would in fact return 0

Update your implementation as follows to use a <= comparison.

for(int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
                addressFragments.add(address.getAddressLine(i));

Post a Comment for "Android Getmaxaddresslineindex Returns 0 For Line 1"