Hi all !
I want to share with you the solution to a weird problem I was having.
I think it was mostly due to my misunderstanding of the way
ItemizedOverlay works. I couldn't find the sources, and the usage is
not much documented. Hopefully I could connect the debugger and dig...
I have a dynamic ItemizedOverlay, which means that from time to time I
update the list of items inside it, removing some and adding others.
When I would sometimes click on an item, than update the items, than
click again on another item, an ArrayIndexOutOfBoundsException was
thrown.
I thought that calling the "populate()" method was enought to get the
Overlay updated, and that I only needed to override the createItem(int
i) and size() methods.
However, one should also call this :
setLastFocusedIndex(-1);
The ItemizedOverlay remembers the index of the last focused item, and
does some stuff with it when you focus on a new item (by clicking on
it for instance). If you change the list of items, then the
"lastFocusedIndex" has no more meaning.
I think that this should be written in the documentation, or that
setLastFocusedIndex(-1); should be called from the populate() method.
One more thing : I think that the minimal touchable bounds of a
drawable should be customizable. I was playing with the map on the
emulator, and I was surprised that click quite far away from a pin
would still get it "tapped". I realized that the minimal box for an
item is 100*100 pixels, which is quite large...
Here is a sample code to show how to use a dynamic ItemizedOverlay,
and to customize the minimal size of a box (I have simplified it from
my real class, to show only the interesting stuff).
public class FaultStationOverlay extends
ItemizedOverlay<FaultStationItem> {
private ArrayList<FaultStationItem> faultStations
= new
ArrayList<FaultStationItem>();
private Drawable normalMarker;
private Drawable fewFaultsMarker;
private Drawable
muchFaultsMarker;
private Rect touchableBounds
= new Rect();
private static final int MIN_TOUCHABLE_WIDTH
= 10;
private static final int MIN_TOUCHABLE_HEIGHT
= 10;
public FaultStationOverlay(Drawable normalMarker, Drawable
fewFaultsMarker,
Drawable muchFaultsMarker) {
super(normalMarker);
this.normalMarker = boundCenterBottom(normalMarker);
this.fewFaultsMarker = boundCenterBottom(fewFaultsMarker);
this.muchFaultsMarker = boundCenterBottom(muchFaultsMarker);
populate();
}
public void updateStations(List<Station> stations) {
faultStations.clear();
for (Station station : stations) {
FaultStationItem stationItem = new
FaultStationItem(station);
switch (stationItem.getState()) {
case FaultStationItem.STATE_NORMAL:
stationItem.setMarker(normalMarker);
break;
case FaultStationItem.STATE_FEW_FAULTS:
stationItem.setMarker(fewFaultsMarker);
break;
case FaultStationItem.STATE_MUCH_FAULTS:
stationItem.setMarker(muchFaultsMarker);
break;
default:
throw new IllegalStateException("stationItem
should have one of
the three available states");
}
faultStations.add(stationItem);
}
setLastFocusedIndex(-1);
populate();
}
@Override
protected boolean hitTest(FaultStationItem item, Drawable marker, int
hitX, int hitY) {
//The code written here was deduced from what happens
in the debugger ;-) .
Rect bounds = marker.getBounds();
int width = bounds.width();
int height = bounds.height();
int centerX = bounds.centerX();
int centerY = bounds.centerY();
int touchWidth = Math.max(MIN_TOUCHABLE_WIDTH, width);
int touchLeft = centerX - touchWidth / 2;
int touchHeight = Math.max(MIN_TOUCHABLE_HEIGHT, height);
int touchTop = centerY - touchHeight / 2;
touchableBounds.set(touchLeft, touchTop, touchLeft + touchWidth,
touchTop + touchHeight);
return touchableBounds.contains(hitX, hitY);
}
@Override
protected FaultStationItem createItem(int i) {
return faultStations.get(i);
}
@Override
public int size() {
return faultStations.size();
}
}
Regards,
Piwaï
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---