package com.example.android.sunshine.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
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 String readLocationFromSettings() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
        final String locKey = getString(R.string.pref_location_key);
        return settings.getString(locKey, "");  // default set in xml
    }

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

    @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);
        }
    }
}

@@ -71,50 +71,8 @@
 nt;%0A
-import android.content.SharedPreferences;%0A
 impo
@@ -153,53 +153,8 @@
 le;%0A
-import android.preference.PreferenceManager;%0A
 impo
@@ -2253,301 +2253,8 @@
  %7D%0A%0A
-    private String readLocationFromSettings() %7B%0A        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());%0A        final String locKey = getString(R.string.pref_location_key);%0A        return settings.getString(locKey, %22%22);  // default set in xml%0A    %7D%0A%0A
     
@@ -2280,24 +2280,24 @@
 Weather() %7B%0A
+
         Fetc
@@ -2376,33 +2376,50 @@
 ute(
-readLocationFromSettings(
+Utility.getPreferredLocation(getActivity()
 ));%0A

Remove duplication