android ArrayList is empty after getter method Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of android ArrayList is empty after getter method without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

Answer

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.

We are here to answer your question about android ArrayList is empty after getter method - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji