package com.example.android.sunshine.app;

import android.annotation.TargetApi;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
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.ListView;

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

/**
 * Created by Rachel on 9/26/2015.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ForecastFragment extends Fragment {

    ForecastAdapter foreCastEntriesAdapter;

    public ForecastFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add this line in order for this fragment to handle menu events.
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.forecastfragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_refresh) {
            updateWeather();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

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

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        String locationSetting = Utility.getPreferredLocation(getActivity());
        // Sort order:  Ascending, by date.
        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC";
        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(
                locationSetting, System.currentTimeMillis());

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

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

        ListView listOfForecasts = (ListView) rootView.findViewById(R.id.listview_forecast);
        listOfForecasts.setAdapter(foreCastEntriesAdapter);

        return rootView;
    }

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

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

@@ -220,16 +220,151 @@
 agment;%0A
+import android.support.v4.app.LoaderManager;%0Aimport android.support.v4.content.CursorLoader;%0Aimport android.support.v4.content.Loader;%0A
 import a
@@ -766,16 +766,66 @@
 ragment 
+implements LoaderManager.LoaderCallbacks%3CCursor%3E  
 %7B%0A%0A    F
@@ -863,16 +863,53 @@
 dapter;%0A
+    private final int  LOADER_ID = 0;
 %0A    pub
@@ -2010,37 +2010,16 @@
 tainer,%0A
-                     
         
@@ -2044,17 +2044,16 @@
 ceState)
- 
 %7B%0A%0A     
@@ -2134,502 +2134,8 @@
 se);
-%0A        String locationSetting = Utility.getPreferredLocation(getActivity());%0A        // Sort order:  Ascending, by date.%0A        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22;%0A        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(%0A                locationSetting, System.currentTimeMillis());%0A%0A        Cursor cur = getActivity().getContentResolver().query(weatherForLocationUri,%0A                null, null, null, sortOrder);
 %0A%0A  
@@ -2203,12 +2203,12 @@
 y(),
- cur
+null
 , 0)
@@ -2363,16 +2363,77 @@
 apter);%0A
+        getLoaderManager().initLoader(LOADER_ID, null, this);
 %0A       
@@ -2791,11 +2791,1066 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public Loader%3CCursor%3E onCreateLoader(int id, Bundle args) %7B%0A        String locationSetting = Utility.getPreferredLocation(getActivity());%0A        // Sort order:  Ascending, by date.%0A        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22;%0A%0A        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(%0A                locationSetting, System.currentTimeMillis());%0A%0A        return new CursorLoader(getActivity(),%0A                                weatherForLocationUri,%0A                                null,%0A                                WeatherContract.LocationEntry.COLUMN_LOCATION_SETTING + %22= ?%22,%0A                                new String%5B%5D%7BlocationSetting%7D,%0A                                sortOrder);%0A    %7D%0A%0A    @Override%0A    public void onLoadFinished(Loader%3CCursor%3E loader, Cursor data) %7B%0A        foreCastEntriesAdapter.swapCursor(data);%0A    %7D%0A%0A    @Override%0A    public void onLoaderReset(Loader%3CCursor%3E loader) %7B%0A        foreCastEntriesAdapter.swapCursor(null);%0A    %7D
 %0A%7D%0A

Add cursor loader to take database operations off the main thread