package com.example.android.sunshine.app.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.android.sunshine.app.R;
import com.example.android.sunshine.app.task.FetchWeatherTask;

import java.util.ArrayList;

public class ForecastFragment extends Fragment {

    private ArrayAdapter<String> forecastAdapter;

    public ForecastFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.forecastfragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_refresh) {
            new FetchWeatherTask(forecastAdapter).execute("94043");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        forecastAdapter = new ArrayAdapter<>(
                getActivity(),
                R.layout.list_item_forecast,
                R.id.list_item_forecast_textview,
                new ArrayList());

        ListView listViewForecast = (ListView) rootView.findViewById(R.id.listview_forecast);
        listViewForecast.setAdapter(forecastAdapter);
        listViewForecast.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String forecast = forecastAdapter.getItem(i);
                Toast toast = Toast.makeText(container.getContext(), forecast, Toast.LENGTH_SHORT);
                toast.show();
            }
        });

        return rootView;
    }

}

@@ -629,85 +629,42 @@
 ate 
-ArrayAdapter%3CString%3E forecastAdapter;%0A%0A    public ForecastFragment() %7B%0A%0A    %7D
+FetchWeatherTask fetchWeatherTask;
 %0A%0A  
@@ -1086,16 +1086,44 @@
 _refresh
+ && fetchWeatherTask != null
 ) %7B%0A    
@@ -1134,13 +1134,9 @@
     
-new F
+f
 etch
@@ -1146,33 +1146,16 @@
 therTask
-(forecastAdapter)
 .execute
@@ -1502,16 +1502,43 @@
 %0A       
+ final ArrayAdapter%3CString%3E
  forecas
@@ -1534,24 +1534,28 @@
 ng%3E forecast
+List
 Adapter = ne
@@ -1731,16 +1731,86 @@
 List());
+%0A        fetchWeatherTask = new FetchWeatherTask(forecastListAdapter);
 %0A%0A      
@@ -1937,24 +1937,28 @@
 ter(forecast
+List
 Adapter);%0A  
@@ -2199,16 +2199,20 @@
 forecast
+List
 Adapter.
@@ -2248,52 +2248,29 @@
 oast
- toast = Toast.makeText(container.getContext
+.makeText(getActivity
 (), 
@@ -2302,31 +2302,8 @@
 ORT)
-;%0A                toast
 .sho

Initialise task on view create