2012-04-07 51 views
0

我希望将标题和片段值以外的内容传递给OverlayItem,通过一些搜索,我发现为OverlayItem创建子类可能是解决方案。但我不知道如何写它。Android为OverlayItem创建子类

VenueMapActivity.class

public void drawVenue(){ 
     ........ 
     Drawable marker = getResources().getDrawable(R.drawable.mall2); 
     VenueMapOverlay venuePos = new VenueMapOverlay(marker,mapView); 
     String getAdd; 

     StringBuilder strReturnedAddress = new StringBuilder("Address: "); 
     try { 
      // Getting Array data from php 

      venues = json.getJSONArray(TAG_VENUE); 
      GeoPoint[] mallCoords = new GeoPoint[venues.length()]; 
      // looping through All data 
      for(int i = 0; i < venues.length(); i++){ 
       .... 
       mallCoords[i] = new GeoPoint((int)(lat * 1E6),(int)(lng *1E6)); 
       List<Overlay> overlays = mapView.getOverlays(); 
        // The custom of overlayItem 
       OverlayItem overlayItem = new CustomItem(mallCoords[i], name, strReturnedAddress.toString(), vid); 
       venuePos.addOverlay(overlayItem); 
       venuePos.setCurrentLocation(currentLocation); 
       overlays.add(venuePos);  
      } 

     } 
     catch(JSONException e){ 
      e.printStackTrace(); 
     } 
} 
//To allow intent when balloon is clicked 
public void startCustomActivity(Context context, String tmp){ 
     Intent Details = new Intent(context, InfoActivity.class); 
     Details.putExtra("Id", tmp); 
     context.startActivity(Details); 
    } 
class CustomItem extends OverlayItem { 

     String venueid =null; 
     CustomItem(GeoPoint pt, String name, String snippet, 
        String venueid) { 
      super(pt, name, snippet); 
      this.venueid = venueid ; 

     } 

VenueMapOverlay.class

public class VenueMapOverlay extends BalloonItemizedOverlay<OverlayItem> { 

private Context mContext; 
private ArrayList<OverlayItem> venues = new ArrayList<OverlayItem>(); 
private Location currentLocation; 

public VenueMapOverlay(Drawable defaultMarker, MapView mapView) { 
    super(boundCenter(defaultMarker), mapView); 

    mContext = mapView.getContext(); 
} 

@Override 
protected OverlayItem createItem(int i) { 
    // TODO Auto-generated method stub 
    return venues.get(i); 
} 

@Override 
public int size() { 
    // TODO Auto-generated method stub 
    return venues.size(); 
} 

public void addOverlay(OverlayItem overlay) { 
    venues.add(overlay); 
    populate(); 
} 

public void setCurrentLocation(Location loc){ 
    this.currentLocation = loc; 
} 

public Location convertGpToLoc(GeoPoint gp){ 
    Location convertedLocation = new Location(""); 

    convertedLocation.setLatitude(gp.getLatitudeE6()/1e6); 
    convertedLocation.setLongitude(gp.getLongitudeE6()/1e6); 

    return convertedLocation; 
} 


@Override 
protected boolean onBalloonTap(int index, OverlayItem item) { 
    String tmp = venues.get(index).getTitle();  
    VenueMapActivity sub = new VenueMapActivity(); 
    sub.startCustomActivity(mContext, tmp); 
    return true; 
} 
} 

在OnBalloonTap方法,我如何得到venueid的价值?
请帮我一把。 Thankyou。

回答

0

试试这个:

OverlayItem item = venues.get(index); 
if(item instanceof CustomItem) { 
    CustomItem cstItem = (CustomItem) item; 
    String myId = cstItem.getVenueId(); 
} 

您可以编写定制项目的方法,例如getVenueId()

class CustomItem extends OverlayItem { 

    String venueid = null; 
    CustomItem(GeoPoint pt, String name, String snippet, 
       String venueid) { 

     super(pt, name, snippet); 
     this.venueid = venueid ; 
    } 

    String getVenueId() 
    { 
     return venueid; 
    } 
} 
+0

嗨skywall,运行良好的代码,但我如何得到的值(OverlayItem overlayItem = new CustomItem(mallCoords [i],name,strReturnedAddress.toString(),vid);' 对于我们写的geTitle()和片段是getSnippet()。 'cstItem.venueid = ...'中的 我应该写什么来获得vid? – Eric 2012-04-07 17:04:27

+0

现在有效〜 感谢您的帮助。 Tq非常。 – Eric 2012-04-07 19:08:17