title: Updating arrayAdapter of a view from outside of the Fragment class
body: <p>I'm following Udacity's Developing Android Apps course, which is quite good till now. </p>
  
  <p>Just for trying new things out, I decided to make a few changes in the code to tidy it up. </p>
  
  <p>Original code basically contains a class extends Android's Fragment class, which also includes another class to get weather data from openweathermap.org API, in a seperate thread. </p>
  
  <p>I decided to move communication, presentation and API conversion features on seperate classes, because it was way too cluttered initially (again, this is just for self-education purposes. Original code works just fine). </p>
  
  <p>Now I have a ForecastFragment class: </p>
  
  <pre><code>public class ForecastFragment extends Fragment {
  
      ArrayAdapter&lt;String&gt; mForecastAdapter;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.fragment_main, container, false);
          //This is just a placeholder until we fetch the real data from web
          String[] data = {"Fetching data from server"};
  
          List&lt;String&gt; forecast = new ArrayList&lt;String&gt;(Arrays.asList(data));
  
          mForecastAdapter = new ArrayAdapter(
                  getActivity(),
                  R.layout.list_item_forecast,
                  R.id.list_item_forecast_textview,
                  data);
  
          ListView listView = (ListView) rootView.findViewById(R.id.listViewForecast);
          listView.setAdapter(mForecastAdapter);
  
          return rootView;
      }
  }
  </code></pre>
  
  <p>I have another two other classes: </p>
  
  <p>One that gets the pure JSON from the server </p>
  
  <pre><code>public class Comm extends AsyncTask&lt;String, Void, String&gt; {
  
      protected String doInBackground(String... params) {
          //Network operations 
          return responseFromServer;
      }
  
     protected void onPostExecute(String s) {
          super.onPostExecute(s);
          //Here should come the code which should update the view
      }
  }
  </code></pre>
  
  <p>And another one which converts JSON to a String[] (<code>ApiConverter</code> class). I'm skipping the details of this one, because it works just fine. It basically takes the raw JSON string and returns a string array. </p>
  
  <p>Now, the problem is, since Comm class does its main work on another thread, I have to update the view in Comm.onPostExecute(). Otherwise main thread is executing all code before Comm class do its thing and I get an empty string. </p>
  
  <p>I thinked about copying the ForecastFragment's onCreateView code to Comm class, but it felt like I'm violating the DRY principle. besides, ForecastFragment class is already instantiating a copy of the Array Adapter, instantiating another with a copied code is a waste of resources. </p>
  
  <p>I have clumsly tried to write a code to get the current instance of ForecastFragment, but hardly failed. </p>
  
  <p>I also tried to change the ForecastFragment to a singleton, following the instructions <a href="http://www.tutorialspoint.com/java/java_using_singleton.htm" rel="nofollow">here</a>. It seems parent Fragment class prevents me from doing it (and I am not certain even if I could do it, I could accomplished what I want). </p>
  
  <p>I can write Comm class as a sub-class of ForecastFragment and reach the class variable from there, but I'd like to know if there is a better way while keeping the classes seperate. </p>
  
  <p>So, what would be a reasonable way to accomplish this while keeping the communication and presentation classes seperate? </p>