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.ListView;

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


/**
 * A placeholder fragment containing a simple view.
 */
public class ForecastFragment extends Fragment {
    public static final String FORECAST_STRING = "FORECAST_STRING";
    private ForecastAdapter mForecastAdapter;

    public ForecastFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        final Uri locationUri = WeatherContract.WeatherEntry.buildWeatherLocation(
                Utility.getPreferredLocation(getActivity()));
        final String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC";
        final Cursor cursor = getActivity().getContentResolver().query(
                locationUri, null, null, null, sortOrder);
        final int flags = 0;
        mForecastAdapter = new ForecastAdapter(getActivity(), cursor, flags);
        ListView lv = (ListView) rootView.findViewById(R.id.listview_forecast);
        if (lv==null) { return rootView; }
        lv.setAdapter(mForecastAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //String forecast = mForecastAdapter.getItem(position);
                Intent i = new Intent(getActivity(), DetailActivity.class);
                //i.putExtra(FORECAST_STRING, forecast);
                startActivity(i);
            }
        });
        return rootView;
    }

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

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

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_refresh:
                updateWeather();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

@@ -438,16 +438,151 @@
 istView;
+%0Aimport android.support.v4.app.LoaderManager;%0Aimport android.support.v4.content.Loader;%0Aimport android.support.v4.content.CursorLoader;
 %0A%0Aimport
@@ -745,16 +745,65 @@
 Fragment
+ implements LoaderManager.LoaderCallbacks%3CCursor%3E
  %7B%0A    p
@@ -865,16 +865,70 @@
 TRING%22;%0A
+    private static final int FORECAST_LOADER_ID = 0;%0A%0A
     priv
@@ -997,32 +997,254 @@
 ment() %7B%0A    %7D%0A%0A
+    @Override%0A    public void onActivityCreated(Bundle savedInstanceState) %7B%0A        super.onActivityCreated(savedInstanceState);%0A        getLoaderManager().initLoader(FORECAST_LOADER_ID, savedInstanceState, this);%0A    %7D%0A%0A
     @Override%0A  
@@ -1495,397 +1495,8 @@
 e);%0A
-        final Uri locationUri = WeatherContract.WeatherEntry.buildWeatherLocation(%0A                Utility.getPreferredLocation(getActivity()));%0A        final String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22;%0A        final Cursor cursor = getActivity().getContentResolver().query(%0A                locationUri, null, null, null, sortOrder);%0A        final int flags = 0;%0A
     
@@ -1557,21 +1557,15 @@
 (), 
-cursor, flags
+null, 0
 );%0A 
@@ -1661,17 +1661,31 @@
  (lv
+ 
 ==
+ 
 null) %7B
+%0A           
  ret
@@ -1697,16 +1697,24 @@
 ootView;
+%0A       
  %7D%0A     
@@ -3001,16 +3001,16 @@
 (item);%0A
-
         
@@ -3016,11 +3016,885 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public Loader%3CCursor%3E onCreateLoader(int id, Bundle args) %7B%0A        final Uri weatherUri = WeatherContract.WeatherEntry.buildWeatherLocation(%0A                Utility.getPreferredLocation(getActivity()));%0A        final String%5B%5D projection = null, selectionArgs = null;%0A        final String selection = null;%0A        final String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22;%0A        return new CursorLoader(%0A                getActivity(),%0A                weatherUri,%0A                projection,%0A                selection,%0A                selectionArgs,%0A                sortOrder);%0A    %7D%0A%0A    @Override%0A    public void onLoadFinished(Loader%3CCursor%3E loader, Cursor data) %7B%0A        mForecastAdapter.swapCursor(data);%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