Back to project page SunshineApp.
The source code is released under:
Apache License
If you think the Android project SunshineApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package sunshine.ferdyrodriguez.com.sunshine; /*from w ww .java2s . c o m*/ import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; 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 android.widget.TextView; import java.util.Date; import sunshine.ferdyrodriguez.com.sunshine.data.WeatherContract; import sunshine.ferdyrodriguez.com.sunshine.data.WeatherContract.WeatherEntry; /** * A simple {@link Fragment} subclass. * Activities that contain this fragment must implement the * {@link 'ForecastFragment. OnFragmentInteractionListener} interface * to handle interaction events. * Use the {@link ForecastFragment # newInstance} factory method to * create an instance of this fragment. * */ public class ForecastFragment extends Fragment implements LoaderCallbacks<Cursor> { private String mLocation; private static final int FORECAST_LOADER = 0; public static final String[] FORECAST_COLUMNS = { WeatherContract.WeatherEntry.TABLE_NAME + "." + WeatherContract.WeatherEntry._ID, WeatherContract.WeatherEntry.COLUMN_DATETEXT, WeatherContract.WeatherEntry.COLUMN_SHORT_DESC, WeatherContract.WeatherEntry.COLUMN_MAX_TEMP, WeatherContract.WeatherEntry.COLUMN_MIN_TEMP, WeatherContract.LocationEntry.COLUMN_LOC_SETTING, WeatherContract.WeatherEntry.COLUMN_WEATHER_ID }; public static final int COL_WEATHER_ID = 0; public static final int COL_WEATHER_DATE = 1; public static final int COL_WEATHER_DESC = 2; public static final int COL_WEATHER_MAX_TEMP = 3; public static final int COL_WEATHER_MIN_TEMP = 4; public static final int COL_LOC_SETTING = 5; public static final int COL_WEATHER_CONDITION_ID = 6; private ForecastAdapter mForecastAdapter; public ForecastFragment(){ } @Override public void onActivityCreated(Bundle savedInstanceState) { getLoaderManager().initLoader(FORECAST_LOADER, null, this); super.onActivityCreated(savedInstanceState); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.forecastfragment, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_refresh){ updateWeather(); return true; } return super.onOptionsItemSelected(item); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mForecastAdapter = new ForecastAdapter(getActivity(), null, 0); View rootView = inflater.inflate(R.layout.fragment_main, container, false); ListView listView = (ListView)rootView.findViewById(R.id.listview_forecast); listView.setAdapter(mForecastAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) { Cursor cursor = mForecastAdapter.getCursor(); if (cursor != null && cursor.moveToPosition(position)) { Intent intent = new Intent(getActivity(), DetailActivity.class) .putExtra(DetailActivity.DATE_KEY, cursor.getString(COL_WEATHER_DATE)); startActivity(intent); } } }); return rootView; } @Override public void onResume() { super.onResume(); if (mLocation != null && !mLocation.equals(Utility.getPreferredLocation(getActivity()))) { getLoaderManager().restartLoader(FORECAST_LOADER, null, this); } } @Override public void onStart() { super.onStart(); } private void updateWeather() { String location = Utility.getPreferredLocation(getActivity()); new FetchWeatherTask(getActivity()).execute(location); } @Override public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { String startDate = WeatherContract.getDbDateString(new Date()); // Sort order: Ascending, by date. String sortOrder = WeatherEntry.COLUMN_DATETEXT + " ASC"; mLocation = Utility.getPreferredLocation(getActivity()); Uri weatherForLocationUri = WeatherEntry.buildWeatherLocationWithStartDate( mLocation, startDate); // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. return new CursorLoader( getActivity(), weatherForLocationUri, FORECAST_COLUMNS, null, null, sortOrder ); } @Override public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor data) { mForecastAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> cursorLoader) { mForecastAdapter.swapCursor(null); } }