简体   繁体   English

如何从另一个类访问对象(例如ArrayList)?

[英]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.

相关问题 如何将键从一个哈希图中关联到另一个哈希图中(例如,学生到教室)? - How can I associate keys from a hashmap to another hashmap (e.g. students to a classroom)? 如何从另一个类访问 ArrayList? - How can I access to an ArrayList from another class? 在 Java 正则表达式中,如何获取字符类(例如 [az])以匹配 - 减号? - In a java regex, how can I get a character class e.g. [a-z] to match a - minus sign? 我如何从Locale.getISOCountries()获得“ en_US”; - How can I get e.g. “en_US” from Locale.getISOCountries(); 如何从 Java 的右侧减少有限的 stream(例如 ArrayList)? - How to reduce a limited stream (e.g. an ArrayList) from right in Java? 如何直接从另一个类访问变量(即不使用类名或对象)? - How to access variable from another class directly(i.e. without using class name or object)? 如何将 object 从一个 class 存储到另一个 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 的 arraylist 中 - How to I store an object in an arraylist from one class in another class 如何计算出现在Android类中的每种变量类型的数量? 例如int,string等 - How can I count the number each variable type, appears in an Android class? e.g. int, string etc 我如何从另一个 class 访问 MainActivity object - How can i access a MainActivity object from another class 如何从另一个类访问这个 ArrayList? - How do I access this ArrayList from another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM