Back to project page Sunshine.
The source code is released under:
Apache License
If you think the Android project Sunshine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2014 The Android Open Source Project */* ww w . java 2s. c o m*/ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.kylelehman.sunshine; 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.support.v4.widget.SimpleCursorAdapter; 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 com.example.kylelehman.sunshine.data.WeatherContract; import com.example.kylelehman.sunshine.data.WeatherContract.LocationEntry; import com.example.kylelehman.sunshine.data.WeatherContract.WeatherEntry; import java.util.Date; /** * Encapsulates fetching the forecast and displaying it as a {@link ListView} layout. */ public class ForecastFragment extends Fragment implements LoaderCallbacks<Cursor> { private SimpleCursorAdapter mForecastAdapter; private static final int FORECAST_LOADER = 0; private String mLocation; // For the forecast view we're showing only a small subset of the stored data. // Specify the columns we need. private static final String[] FORECAST_COLUMNS = { // In this case the id needs to be fully qualified with a table name, since // the content provider joins the location & weather tables in the background // (both have an _id column) // On the one hand, that's annoying. On the other, you can search the weather table // using the location set by the user, which is only in the Location table. // So the convenience is worth it. WeatherEntry.TABLE_NAME + "." + WeatherEntry._ID, WeatherEntry.COLUMN_DATETEXT, WeatherEntry.COLUMN_SHORT_DESC, WeatherEntry.COLUMN_MAX_TEMP, WeatherEntry.COLUMN_MIN_TEMP, LocationEntry.COLUMN_LOCATION_SETTING }; // These indices are tied to FORECAST_COLUMNS. If FORECAST_COLUMNS changes, these // must change. 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_LOCATION_SETTING = 5; 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) { 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(); if (id == R.id.action_refresh) { updateWeather(); return true; } return super.onOptionsItemSelected(item); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // The SimpleCursorAdapter will take data from the database through the // Loader and use it to populate the ListView it's attached to. mForecastAdapter = new SimpleCursorAdapter( getActivity(), R.layout.list_item_forecast, null, // the column names to use to fill the textviews new String[]{WeatherEntry.COLUMN_DATETEXT, WeatherEntry.COLUMN_SHORT_DESC, WeatherEntry.COLUMN_MAX_TEMP, WeatherEntry.COLUMN_MIN_TEMP }, // the textviews to fill with the data pulled from the columns above new int[]{R.id.list_item_date_textview, R.id.list_item_forecast_textview, R.id.list_item_high_textview, R.id.list_item_low_textview }, 0 ); mForecastAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { boolean isMetric = Utility.isMetric(getActivity()); switch (columnIndex) { case COL_WEATHER_MAX_TEMP: case COL_WEATHER_MIN_TEMP: { // we have to do some formatting and possibly a conversion ((TextView) view).setText(Utility.formatTemperature( cursor.getDouble(columnIndex), isMetric)); return true; } case COL_WEATHER_DATE: { String dateString = cursor.getString(columnIndex); TextView dateView = (TextView) view; dateView.setText(Utility.formatDate(dateString)); return true; } } return false; } }); View rootView = inflater.inflate(R.layout.fragment_main, container, false); // Get a reference to the ListView, and attach this adapter to it. 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 onActivityCreated(Bundle savedInstanceState) { getLoaderManager().initLoader(FORECAST_LOADER, null, this); super.onActivityCreated(savedInstanceState); } private void updateWeather() { String location = Utility.getPreferredLocation(getActivity()); new FetchWeatherTask(getActivity()).execute(location); } @Override public void onResume() { super.onResume(); if (mLocation != null && !Utility.getPreferredLocation(getActivity()).equals(mLocation)) { getLoaderManager().restartLoader(FORECAST_LOADER, null, this); } } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { // This is called when a new Loader needs to be created. This // fragment only uses one loader, so we don't care about checking the id. // To only show current and future dates, get the String representation for today, // and filter the query to return weather only for dates after or including today. // Only return data after today. 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> loader, Cursor data) { mForecastAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { mForecastAdapter.swapCursor(null); } }