package com.example.android.sunshine.app;

import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
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.TextView;

import com.example.android.sunshine.app.data.WeatherContract;

import java.util.ArrayList;

public class MainFragment extends Fragment {

    public final static String EXTRA_MESSAGE = "com.example.android.sunshine.app.MESSAGE";

    private ForecastAdapter mForecastAdapter;

    public MainFragment() {
    }

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

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

    private void updateWeather() {
        FetchWeatherTask weatherTask = new FetchWeatherTask(getActivity());
        String locationPref = Utility.getPreferredLocation(getActivity());
        weatherTask.execute(locationPref);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        String locationPref = Utility.getPreferredLocation(getActivity());

        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC";

        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(
                locationPref, System.currentTimeMillis());

        Cursor cur = getActivity().getContentResolver().query(weatherForLocationUri,
                null, null, null, sortOrder);

        mForecastAdapter = new ForecastAdapter(getActivity(), cur, 0);

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);

        listView.setAdapter(mForecastAdapter);

        return rootView;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_refresh) {
            updateWeather();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -40,39 +40,8 @@
 p;%0A%0A
-import android.content.Intent;%0A
 impo
@@ -177,304 +177,276 @@
 oid.
-view.LayoutInflater;%0Aimport android.view.Menu;%0Aimport android.view.MenuInflater;%0Aimport android.view.MenuItem;%0Aimport android.view.View;%0Aimport android.view.ViewGroup;%0Aimport android.widget.AdapterView;%0Aimport android.widget.ArrayAdapter;%0Aimport android.widget.ListView;%0Aimport android.widget.Tex
+support.v4.app.LoaderManager;%0Aimport android.support.v4.content.CursorLoader;%0Aimport android.support.v4.content.Loader;%0Aimport android.view.LayoutInflater;%0Aimport android.view.MenuItem;%0Aimport android.view.View;%0Aimport android.view.ViewGroup;%0Aimport android.widget.Lis
 tVie
@@ -516,37 +516,8 @@
 t;%0A%0A
-import java.util.ArrayList;%0A%0A
 publ
@@ -555,17 +555,117 @@
 ragment 
-%7B
+implements LoaderManager.LoaderCallbacks%3CCursor%3E %7B%0A%0A    private static final int FORECAST_LOADER = 0;
 %0A%0A    pu
@@ -976,32 +976,237 @@
 eState);%0A    %7D%0A%0A
+    @Override%0A    public void onActivityCreated(Bundle savedInstanceState) %7B%0A        getLoaderManager().initLoader(FORECAST_LOADER, null, this);%0A        super.onActivityCreated(savedInstanceState);%0A    %7D%0A%0A
     @Override%0A  
@@ -1675,456 +1675,8 @@
 ) %7B%0A
-%0A        String locationPref = Utility.getPreferredLocation(getActivity());%0A%0A        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22;%0A%0A        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(%0A                locationPref, System.currentTimeMillis());%0A%0A        Cursor cur = getActivity().getContentResolver().query(weatherForLocationUri,%0A                null, null, null, sortOrder);%0A%0A
     
@@ -1737,18 +1737,32 @@
 (), 
-cur, 0
+null, FORECAST_LOADER
 );%0A
-%0A
     
@@ -1841,17 +1841,16 @@
 false);%0A
-%0A
         
@@ -1927,17 +1927,16 @@
 ecast);%0A
-%0A
         
@@ -1974,17 +1974,16 @@
 apter);%0A
-%0A
         
@@ -2252,11 +2252,765 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public Loader%3CCursor%3E onCreateLoader(int id, Bundle args) %7B%0A        String locationPref = Utility.getPreferredLocation(getActivity());%0A        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(%0A                locationPref, System.currentTimeMillis());%0A        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22;%0A        return new CursorLoader(getActivity(), weatherForLocationUri, null, null, null, sortOrder);%0A    %7D%0A%0A    @Override%0A    public void onLoadFinished(Loader%3CCursor%3E loader, Cursor cursor) %7B%0A        mForecastAdapter.swapCursor(cursor);%0A%0A    %7D%0A%0A    @Override%0A    public void onLoaderReset(Loader%3CCursor%3E loader) %7B%0A        mForecastAdapter.swapCursor(null);%0A    %7D
 %0A%7D%0A

Use CursorLoader in MainFragment