Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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<address.getMaxAddressLineIndex(). However, it is broken somehow.

enter image description here

I don't know if it is due to the new Google Api or something else.

Can someone please confirm me here what is going on?

like image 283
codebased Avatar asked Jul 08 '17 07:07

codebased


2 Answers

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

like image 103
Pasan Randula Eramusugoda Avatar answered Oct 24 '22 12:10

Pasan Randula Eramusugoda


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

like image 34
mdicosimo Avatar answered Oct 24 '22 11:10

mdicosimo