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

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
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.activity.DetailActivity;
import com.example.android.sunshine.app.task.FetchWeatherTask;

import java.util.ArrayList;

public class ForecastFragment extends Fragment {

    private ArrayAdapter<String> forecastListAdapter;

    @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 && forecastListAdapter != null) {
            populateForecastList();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStart() {
        super.onStart();
        populateForecastList();
    }

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

        forecastListAdapter = 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(forecastListAdapter);
        listViewForecast.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String forecast = forecastListAdapter.getItem(i);
                Intent intent = new Intent(getActivity(), DetailActivity.class)
                        .putExtra(Intent.EXTRA_INTENT, forecast);
                startActivity(intent);
            }
        });

        return rootView;
    }

    private AsyncTask<String, Void, String[]> populateForecastList() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String location = preferences.getString(getString(R.string.pref_location_key), getString(R.string.pref_location_default));
        String temperature = preferences.getString(getString(R.string.pref_temperature_key), getString(R.string.pref_temperature_default));
        return new FetchWeatherTask(getActivity(), forecastListAdapter).execute(location, temperature);
    }

}

@@ -49,40 +49,8 @@
 t;%0A%0A
-import android.content.Context;%0A
 impo
@@ -72,24 +72,24 @@
 ent.Intent;%0A
+
 import andro
@@ -118,16 +118,48 @@
 rences;%0A
+import android.database.Cursor;%0A
 import a
@@ -309,16 +309,126 @@
 oid.
-util.Log
+support.v4.app.LoaderManager;%0Aimport android.support.v4.content.CursorLoader;%0Aimport android.support.v4.content.Loader
 ;%0Aim
@@ -675,24 +675,61 @@
 rayAdapter;%0A
+import android.widget.CursorAdapter;%0A
 import andro
@@ -751,37 +751,8 @@
 iew;
-%0Aimport android.widget.Toast;
 %0A%0Aim
@@ -997,17 +997,113 @@
 ragment 
-%7B
+implements LoaderManager.LoaderCallbacks%3CCursor%3E %7B%0A%0A    public static int WEATHER_LOADER_ID = 42;
 %0A%0A    pr
@@ -1259,24 +1259,24 @@
 anceState);%0A
-
         setH
@@ -1292,24 +1292,108 @@
 Menu(true);%0A
+        getLoaderManager().initLoader(WEATHER_LOADER_ID, savedInstanceState, this);%0A
     %7D%0A%0A    @
@@ -3597,10 +3597,283 @@
 %0A    %7D%0A%0A
+    @Override%0A    public Loader%3CCursor%3E onCreateLoader(int id, Bundle args) %7B%0A        return null;%0A    %7D%0A%0A    @Override%0A    public void onLoadFinished(Loader%3CCursor%3E loader, Cursor data) %7B%0A%0A    %7D%0A%0A    @Override%0A    public void onLoaderReset(Loader%3CCursor%3E loader) %7B%0A%0A    %7D%0A
 %7D%0A

Use loaders