Hi
this can be trigger by not create OverlayItem
//customized ItemizedOverlay
protected class FriendOverLay extends ItemizedOverlay<OverlayItem>
private List<OverlayItem> mItemList = new ArrayList<OverlayItem>();
{
public FriendOverLay(Drawable marker) {
super(marker);
*mItemList.add(new OverlayItem(whatAroundMe.currentPoint , "Title",
"Snippet"));*
populateFriends();
}
public synchronized void populateFriends()
{
mFriendList = DataManager.getInstance().getFriendLocations();
super.populate();
}
@Override
public void draw(Canvas canvas, MapView view, boolean shadow ) {
super.draw(canvas, view, shadow);
boundCenterBottom(marker);
//draw other stuffs...
}
}
}
[EMAIL PROTECTED]
protected OverlayItem createItem(int i) {
return mItemList.get(i);
}
@Override
public int size() {
return mItemList.size();
}*
hope it helps
Peter
elephantbug wrote:
> Hi, Mark,
>
> Here is the code snip:
> ----------------------------------------------
> Main Map Screen
> -----------------------------------------------
> public class MyMapActivity extends MapActivity implements Observer{
>
> //global array of overlay items
> List<OverlayItem> mFriendList = Collections.synchronizedList(new
> ArrayList<OverlayItem>());
>
> public void onCreate(){
> //init...(omit)
> ...
> //add itemized overlay
> myOverLay = new FriendOverLay(marker);
> mMapView.getOverlays().add(myOverLay);
>
> //...}
>
> //observer
> public void update(Observable o, Object args) {
> if(args instanceof Byte)
> {
> Byte event = (Byte)args;
> if(event == EVENT_UPDATES)
> {
> // try to refresh the screen
> runOnUiThread( new Runnable()
> {
> public void run()
> {
> if(myOverLay != null)
> {
>
> myOverLay.populateFriends();
> }
> if(mMapView != null)
>
> mMapView.postInvalidateDelayed(5000);
> }
>
> });
>
> }
> }
> }
>
>
> //customized ItemizedOverlay
> protected class FriendOverLay extends ItemizedOverlay<OverlayItem>
> {
> public FriendOverLay(Drawable marker) {
> super(marker);
>
> populateFriends();
> }
>
> public synchronized void populateFriends()
> {
> mFriendList =
> DataManager.getInstance().getFriendLocations();
> super.populate();
> }
>
> @Override
> public void draw(Canvas canvas, MapView view, boolean shadow ) {
>
> super.draw(canvas, view, shadow);
> boundCenterBottom(marker);
>
> //draw other stuffs...
> }
> }
> }
>
>
> -----------------------------
> The data manager
> -----------------------------
> public class DataManager extends Observable{
>
> private List<OverlayItem> friendStatus =
> Collections.synchronizedList(new ArrayList<OverlayItem>());
>
>
> public List<OverlayItem> getFriendLocations()
> {
> synchronized (friendAccessMutex) {
> return friendStatus;
> }
> }
>
> //will spawn a new thread to update the friendStaus array
> periodically.and call setChange and notify the Map Screen (trigger
> update())...
>
> }
>
> -----------------------------------------------------------------------
>
> Most of time, it works fine. But sometimes ( I have not found the
> pattern yet), it will trigger ArrayOutOfIndexException
> on the ItemizedOverlay.draw() method.
>
> So basically, my question is how to achieve one screen display the
> dynamic data on ItemizedOverlay?
>
> Any input is appreciated.
>
> Thanks.
>
> --elephantbug
>
>
>
> On Sep 24, 6:34 pm, Mark Murphy <[EMAIL PROTECTED]> wrote:
>
>> elephantbug wrote:
>>
>>> I extend the MapActivity and have an ItemizedOverlay which will
>>> display array of items(For example, people) on top of MapView. These
>>> items' location are moving and some new items might be added in or
>>> some might be removed. There is another thread managing these items.
>>>
>>> MapActivity is the observer of these items and update the UI whenever
>>> items changes. It works fine most of time. However, periodically, it
>>> will cause trouble and makes ItemizedOverlay throw ArrayOutofIndex
>>> exceptionrom getIndexToDraw() method.
>>>
>>> Anyone can help me why it might happen and what is the best practice
>>> to support dynamic data bind for ItemizedOverlay?
>>>
>> Without seeing your implementation of your ItemizedOverlay subclass, it
>> is difficult to make many suggestions.
>>
>> Bear in mind that if you change the data in a background thread, the
>> foreground (UI) thread might still be working off of old information.
>> For example, suppose you have six items, and the foreground thread is
>> busy drawing those six items on the map. While that is happening, your
>> background thread removes one item. The foreground thread has no way to
>> know that occurred, so it will try requesting the sixth item...and will
>> fail.
>>
>> Since I don't know how you are telling the MapView that there is changed
>> data in the ItemizedOverlay, I cannot make any particular suggestions of
>> how to get around this problem. What *may* work is to change the
>> contents of your ItemizedOverlay only on the foreground (UI) thread --
>> just have the background thread use a Handler or use runOnUiThread() or
>> something to arrange for the work to get done on the foreground thread.
>>
>> --
>> Mark Murphy (a Commons Guy)http://commonsware.com
>> _The Busy Coder's Guide to Android Development_ Version 1.2 Published!
>>
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---