package com.example.android.popularmoviesapp;

import android.database.Cursor;
import android.os.Bundle;
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.GridView;

import static com.example.android.popularmoviesapp.data.MovieContract.MovieEntry;


public class MoviePostersFragment extends Fragment {
    private static final String LOG_TAG = MoviePostersFragment.class.getSimpleName();
    private MoviePosterAdapter posterAdapter;


    public MoviePostersFragment() {
        // Required empty public constructor
    }

    public static MoviePostersFragment newInstance() {
        Log.d(LOG_TAG, "newInstance");
        return new MoviePostersFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(LOG_TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(LOG_TAG, "onCreateView");

        test();

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

        // attach adapter to GridView
        GridView gridView = (GridView) rootView.findViewById(R.id.grid_view_posters);
        gridView.setAdapter(posterAdapter);

        return rootView;
    }

    @Override
    public void onStart() {
        Log.d(LOG_TAG, "onStart");
        super.onStart();
        (new FetchMovieTask(getContext())).execute(getString(R.string.pref_sort_by_rating));
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        Log.d(LOG_TAG, "onCreateOptionsMenu");
        inflater.inflate(R.menu.movie_posters, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d(LOG_TAG, "onOptionsItemSelected");
        int id = item.getItemId();

        if (id == R.id.action_sort_by_popularity) {
            (new FetchMovieTask(getContext())).execute(getString(R.string.pref_sort_by_popularity));
            test();
            return true;
        } else if (id == R.id.action_sort_by_rating) {
            (new FetchMovieTask(getContext())).execute(getString(R.string.pref_sort_by_rating));
            test();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    // TODO does not update the adapter
    private void test() {
        // TODO sortOrder
        final String sortOrder = null;
        final Cursor cursor = getActivity().getContentResolver().query(MovieEntry.CONTENT_URI,
                null, null, null, sortOrder);

        posterAdapter = new MoviePosterAdapter(getActivity(), cursor, 0);
    }

}

@@ -138,16 +138,151 @@
 agment;%0A
+import android.support.v4.app.LoaderManager;%0Aimport android.support.v4.content.CursorLoader;%0Aimport android.support.v4.content.Loader;%0A
 import a
@@ -648,16 +648,65 @@
 Fragment
+ implements LoaderManager.LoaderCallbacks%3CCursor%3E
  %7B%0A    p
@@ -782,24 +782,72 @@
 mpleName();%0A
+    private static final int MOVIES_LOADER = 0;%0A
     private 
@@ -1498,21 +1498,77 @@
         
-test(
+posterAdapter = new MoviePosterAdapter(getActivity(), null, 0
 );%0A%0A    
@@ -2561,36 +2561,16 @@
 rity));%0A
-            test();%0A
         
@@ -2742,28 +2742,8 @@
 ));%0A
-            test();%0A
     
@@ -2839,70 +2839,286 @@
     
-// TODO does not update the adapter%0A    private void test(
+@Override%0A    public void onActivityCreated(Bundle savedInstanceState) %7B%0A        getLoaderManager().initLoader(MOVIES_LOADER, null, this);%0A        super.onActivityCreated(savedInstanceState);%0A    %7D%0A%0A    @Override%0A    public Loader%3CCursor%3E onCreateLoader(int id, Bundle args
 ) %7B%0A
+%0A
     
@@ -3190,213 +3190,424 @@
     
-final Cursor cursor = getActivity().getContentResolver().query(MovieEntry.CONTENT_URI,%0A                null, null, null, sortOrder);%0A%0A        posterAdapter = new MoviePosterAdapter(getActivity(), cursor, 0
+return new CursorLoader(getActivity(),%0A                MovieEntry.CONTENT_URI,%0A                null,%0A                null,%0A                null,%0A                sortOrder);%0A    %7D%0A%0A    @Override%0A    public void onLoadFinished(Loader%3CCursor%3E loader, Cursor data) %7B%0A        posterAdapter.swapCursor(data);%0A    %7D%0A%0A    @Override%0A    public void onLoaderReset(Loader%3CCursor%3E loader) %7B%0A        posterAdapter.swapCursor(null
 );%0A 
@@ -3611,11 +3611,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Add CursorLoader