Jaume
Jaume

Reputation: 3780

android ArrayList is empty after getter method

I have a class that implements getter and setter methods and related code as follows.

ArrayList<String> viewArray = new ArrayList<String>();

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

from my activity, I am trying to get acces to stored array like:

ArrayList<String> al = new ArrayList<String>();

al = parsedExampleDataSet.getView();

But "al" receives no data. However, when getView() is executed, viewArray is filled properly. What am I missing? Thank you.

Upvotes: 0

Views: 758

Answers (1)

Gray
Gray

Reputation: 116858

Others have make some good comments but I thought I'd take you through the code as I see it.

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;
    }
}

Here's your activity class annotated with some details about the value of 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
    }
}

Please edit your question to explain more what you are having problems with.

Upvotes: 1

Related Questions