【问题标题】:How to get the data that is stored in ArrayList from another class in Java/Android? [duplicate]如何从 Java/Android 中的另一个类获取存储在 ArrayList 中的数据? [复制]
【发布时间】:2017-10-19 03:37:17
【问题描述】:

我是 Java/Android 的新手,我正在尝试做一件事,但我不确定我是否可以做到。

我的问题是:我正在解析一个 Json 并将这个 json 发送到我的班级。一切都是正确的,json 可以正常工作并且数据存储正确。我想做的是从另一个类访问我存储在 arrayList 中的数据,但我不知道该怎么做。

我尝试实现一个单例 java 类,但我无法访问数据。

我说的是例子。如果我创建此方法,我可以访问数据,但我需要将数据从我的 json 传递给该方法。

 public String showOverlay(ArrayList<ScreenEvent> config){
        String show = "";
        String empty = "empty";
        for(ScreenEvent client : config){
            show = client.action;

            if(show.equals("show"))
                return show;
        }
        return empty;
    }

我不想这样做。我希望能够在我的方法中创建 arrayList 的对象:

 public String myMethod(){

         //I want access here to the data of the arrayList

            return empty;
        }

我读取一个 json 并将数据传递到一个 ArrayList 中:

    public static ArrayList<VsClientConfig.ScreenEvent> eventConfig = new ArrayList<VsClientConfig.ScreenEvent>();

       //JSON stuff
       VsClientConfig.ScreenEvent vs = VsClientConfig.ScreenEvent.getScreenEvent(action, className, typeEvent, viewId, colourEvent);
       eventConfig.add(vs);

这是我的课:

public class VsClientConfig{

    public String colour;
    public String height;

    public static class ScreenEvent {
        public String action;
        public String className;
        public String typeEvent;
        public String viewId;
        public String colourEvent;
        private static ScreenEvent miScreenEvent;

        public static ScreenEvent getScreenEvent(String action, String className, String typeEvent, String viewId, String colourEvent) {

            if (miScreenEvent == null) {

                miScreenEvent = new ScreenEvent(action, className, typeEvent, viewId, colourEvent);
            }
            return miScreenEvent;
        }

        private ScreenEvent(String action, String className, String typeEvent, String viewId, String colourEvent) {
            this.action = action;
            this.className = className;
            this.typeEvent = typeEvent;
            this.viewId = viewId;
            this.colourEvent = colourEvent;
        }

    }

    public String myMethod(){

     //I want access here to the data of the arrayList

        return empty;
    }
...

【问题讨论】:

  • 你的 ArrayList 在单例类中吗?
  • @miles 不确定....这是我的代码...
  • 只需编写一个返回列表的getter。如果您需要将其传递给另一个活动,则需要将其设为 Parcelable。

标签: java android arrays arraylist


【解决方案1】:

在 Common 类中创建并初始化静态 arrayList,如下所示:

public class Common{
       public static ArrayList<VsClientConfig.ScreenEvent> eventConfig=new ArrayList<>();
}

然后从任何你想要的地方分配 if:

//JSON stuff
       VsClientConfig.ScreenEvent vs = VsClientConfig.ScreenEvent.getScreenEvent(action, className, typeEvent, viewId, colourEvent);
       Common.eventConfig.add(vs);

现在Common.eventConfig(您的arrayList)将可以通过您的应用程序访问

【讨论】:

    猜你喜欢
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多