我有一个实现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会正确填充。我错过了什么?谢谢。
答案 0 :(得分:1)
其他人已经做了一些很好的评论,但我想我会带你看看代码。
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
}
}
请编辑您的问题,以解释您遇到的问题。