title: How to access ArrayList from another class in Android Java?
body: <p>I'm new to Android and Java but do have some experience in Objective C and iPhone programming. I'm attempting to recreate an app I've already designed for the iPhone and am getting stuck on what should be a simple concept. </p>
  
  <p>In my ParserHandler class I am parsing an XML from a server and putting the data into three separate ArrayList's. The parsing appears to be working fine. When I log and iterate through the ArrayList within my ParserHandler.java class it all works fine.
  (List1.java class has a few string variables and I've declared it like so in the ParserHandler: <code>private List1 theList = new List1();</code> )</p>
  
  <pre><code>for(int i = 0; i&lt;dogArray.size(); i++){
          theList = dogArray.get(i);
          Log.i(TAG, "looping " + i + " " + theList.Name);
          Log.i(TAG, "looping " + i + " " + theList.PhotoUrl);
          Log.i(TAG, "looping " + i + " " + theList.Type);
  
  
          }//this loops fine and has all the data
  </code></pre>
  
  <p>The dogArray is declared like so: <code>public ArrayList&lt;List1&gt; dogArray = new ArrayList&lt;List1&gt;();
  </code>Now I want to access the dogArray from the class DogListView.java so in the onCreate method I attempt to do the following:</p>
  
  <pre><code>@Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.dog_list_view);
          ParserHandler ph = new ParserHandler();
          int d = ph.getNumberofDogs();
          int m = ph.dogArray.size();
          Log.i(TAG, "dog size is:" + d + "and:" + m);}
  </code></pre>
  
  <p>I've tried two different ways and both always return "0" in the log. However the correct size is always logged and all the data is there when the log comes from the ParserHandler.java class. </p>
  
  <p>This is the accessor method in ParserHandler.java.</p>
  
  <pre><code>   public int getNumberofDogs(){
              return dogArray.size();
          }
  </code></pre>
  
  <p>I'd prefer to access the dogArray via accessor method (as this seems to be best practice from what I've gathered) however I'm open to all suggestions.</p>
  
  <p>Thanks in advance!!</p>
  
  <p><strong>EDIT 8/23/12</strong></p>
  
  <p>I ended up solving the problem by declaring my <code>ArrayLists Static</code>. I know this (and <code>public</code>) approach my not be ideal for OOP but i'm going with it. In my ParserHandler.java I declared </p>
  
  <pre><code>public static ArrayList&lt;List1&gt; dogArray = null; 
  public static ArrayList&lt;List1&gt; otherArray = null;
  public static ArrayList&lt;List1&gt; catArray = null;
  </code></pre>
  
  <p>Then begin my SAX parser:</p>
  
  <pre><code>@Override
      public void startElement(String uri, String localName, String qName,
              Attributes attributes) throws SAXException {
          // TODO Auto-generated method stub
          super.startElement(uri, localName, qName, attributes);
          if (qName.equalsIgnoreCase("ArrayOfDataFeedAnimal")){
              catArray = new ArrayList&lt;List1&gt;();
              dogArray = new ArrayList&lt;List1&gt;();
              otherArray = new ArrayList&lt;List1&gt;();
          }else if(qName.equalsIgnoreCase("DataFeedAnimal")){
              theList = new List1();
          }
  </code></pre>
  
  <p>then the rest of my SAX parsing stuff happens. Lastly, from whatever class I want to access the array i simply do that in the static way by <code>ParserHandler.dogArray.size()</code> to get the size of the array. I can now manipulate the array any way i see fit from whatever class i need to get it. </p>
  
  <p>I'm still unclear why creating an instance of the ParserHandler class hasn't worked for me with my parsed ArrayLists because when it worked fine when I tested with a simple <code>int[]</code>. </p>
  
  <p>hopefully this can help someone else in the future.</p>
  
  <p>Thanks again for everyones feedback!</p>
  
  <p>Cheers!</p>