[英]How can I access an object (e.g. ArrayList) from another class?
I am looking for a way that allows me access an object from another class; 我正在寻找一种允许我从另一个类访问对象的方法; both classes are within the same Android activity - OpenStreeMapActivity.java.
这两个类都在同一个Android活动中 - OpenStreeMapActivity.java。 I have:
我有:
ItemizedOverlay.java - Contains the object I want to access and modify: ItemizedOverlay.java - 包含我想要访问和修改的对象:
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
BalloonOverlayView.java - Is where I want to access the object mOverlays: BalloonOverlayView.java - 我想要访问对象mOverlays的位置:
protected void setupView(final Context context, final ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.balloon_overlay, parent);
title = (TextView) v.findViewById(R.id.balloon_item_title);
snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);
// Get ballon_close button and register its listener:
ImageView close = (ImageView) v.findViewById(R.id.balloon_close);
close.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
parent.setVisibility(GONE);
Intent intent = new Intent( );
intent.setClassName( "org.example.openstreetmap", "org.example.openstreetmap.UpdateEntityActivity" );
v.getContext().startActivity(intent);
//HERE I return from UpdateOverlayActivity.java and is where I want to modify *mOverlays*.
}
});
}
Edit: I figured out that it is not true that I return to //HERE. 编辑:我发现我回到这里是不正确的。
In ItemizedOverlay, create a method that provides the object. 在ItemizedOverlay中,创建一个提供对象的方法。
public List<OverlayItem> getOverlays() {
return this.mOverlays;
}
Better if you use the List so, if in the future you want to change the implementation it does not affect your code elsewhere. 如果您使用List更好,那么如果将来您想要更改实现,它不会影响您在其他地方的代码。
You can make the BalloonOverlayView
hold a reference to a list of OverlayItem
object this way: 您可以通过以下方式使
BalloonOverlayView
保持对OverlayItem
对象列表的OverlayItem
:
public class BalloonOverlayView{
List<OverlayItem> items = null;
public BalloonOverlayView(List<OverlayItem> items){
this.items = items;
}
// Now you can use the ItemizedOverlay class from within this class as you wish
public void addItem(OverlayItem item){
items.add(item);
}
public void removeItem(OverlayItem item){
item.remove(item);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.