getter方法之后的android ArrayList为空



我有一个实现getter和setter方法以及相关代码的类,如下所示。

ArrayList<String> viewArray = new ArrayList<String>();
public ArrayList<String> getView() {
return viewArray;
}

从我的活动中,我试图获得存储数组的访问权限,如:

ArrayList<String> al = new ArrayList<String>();
al = parsedExampleDataSet.getView();

但"al"没有收到任何数据。但是,当执行getView()时,viewArray会正确填充。我错过了什么?非常感谢。

其他人发表了一些不错的评论,但我想我会带你浏览我看到的代码。

public class SomeClass {
    // this is local to this class only
    ArrayList<String> viewArray = new ArrayList<String>();
    public void process() {
       // i'm guessing there is some sort of processing method that is called
    }
    public ArrayList<String> getView() {
       return viewArray;
    }
}

以下是您的活动类,其中注释了有关a1:值的一些详细信息

public class YourActivity {
    ArrayList<String> al = new ArrayList<String>();
    public void someMethod() {
        // here a1 will be the same blank List you initialized it with
        // unless someMethod() has been called before or a1 modified elsewhere
        al = parsedExampleDataSet.getView();
        // after the call to getView, a1 is now a reference to SomeClass.viewArray
        // the ArrayList that a1 was initialized with is then garbage collected
    }
}

请编辑您的问题,详细解释您遇到的问题。

最新更新