From dbff5f9982d9ca48bec7dd6bbbd6c8efaf944901 Mon Sep 17 00:00:00 2001 From: Dan Galpin Date: Mon, 25 May 2015 02:57:21 -0700 Subject: [PATCH] Starting point for RecyclerView. --- .../android/sunshine/app/ForecastAdapter.java | 82 +++++++++---------- app/src/main/res/layout/activity_main.xml | 23 +----- 2 files changed, 41 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java b/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java index f02b69c..4df5d55 100644 --- a/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java +++ b/app/src/main/java/com/example/android/sunshine/app/ForecastAdapter.java @@ -43,18 +43,18 @@ public class ForecastAdapter extends CursorAdapter { * Cache of the children views for a forecast list item. */ public static class ViewHolder { - public final ImageView iconView; - public final TextView dateView; - public final TextView descriptionView; - public final TextView highTempView; - public final TextView lowTempView; + public final ImageView mIconView; + public final TextView mDateView; + public final TextView mDescriptionView; + public final TextView mHighTempView; + public final TextView mLowTempView; public ViewHolder(View view) { - iconView = (ImageView) view.findViewById(R.id.list_item_icon); - dateView = (TextView) view.findViewById(R.id.list_item_date_textview); - descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview); - highTempView = (TextView) view.findViewById(R.id.list_item_high_textview); - lowTempView = (TextView) view.findViewById(R.id.list_item_low_textview); + mIconView = (ImageView) view.findViewById(R.id.list_item_icon); + mDateView = (TextView) view.findViewById(R.id.list_item_date_textview); + mDescriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview); + mHighTempView = (TextView) view.findViewById(R.id.list_item_high_textview); + mLowTempView = (TextView) view.findViewById(R.id.list_item_low_textview); } } @@ -91,55 +91,53 @@ public class ForecastAdapter extends CursorAdapter { ViewHolder viewHolder = (ViewHolder) view.getTag(); int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID); - int fallbackIconId; - int viewType = getItemViewType(cursor.getPosition()); - switch (viewType) { - case VIEW_TYPE_TODAY: { - // Get weather icon - fallbackIconId = Utility.getArtResourceForWeatherCondition( - weatherId); + int defaultImage; + + switch (getItemViewType(cursor.getPosition())) { + case VIEW_TYPE_TODAY: + defaultImage = Utility.getArtResourceForWeatherCondition(weatherId); break; - } - default: { - // Get weather icon - fallbackIconId = Utility.getIconResourceForWeatherCondition( - weatherId); - break; - } + default: + defaultImage = Utility.getIconResourceForWeatherCondition(weatherId); } - Glide.with(mContext) - .load(Utility.getArtUrlForWeatherCondition(mContext, weatherId)) - .error(fallbackIconId) - .crossFade() - .into(viewHolder.iconView); + if (Utility.usingLocalGraphics(mContext)) { + viewHolder.mIconView. + setImageResource(defaultImage); + } else { + Glide.with(mContext) + .load(Utility.getArtUrlForWeatherCondition(mContext, weatherId)) + .error(defaultImage) + .crossFade() + .into(viewHolder.mIconView); + } // Read date from cursor long dateInMillis = cursor.getLong(ForecastFragment.COL_WEATHER_DATE); // Find TextView and set formatted date on it - viewHolder.dateView.setText(Utility.getFriendlyDayString(context, dateInMillis)); + viewHolder.mDateView.setText(Utility.getFriendlyDayString(context, dateInMillis)); - // Get description from weather condition ID - String description = Utility.getStringForWeatherCondition(context, weatherId); + // Read weather forecast from cursor + String description = Utility.getStringForWeatherCondition(mContext, weatherId); // Find TextView and set weather forecast on it - viewHolder.descriptionView.setText(description); - viewHolder.descriptionView.setContentDescription(context.getString(R.string.a11y_forecast, description)); + viewHolder.mDescriptionView.setText(description); + viewHolder.mDescriptionView.setContentDescription(mContext.getString(R.string.a11y_forecast, description)); // For accessibility, we don't want a content description for the icon field // because the information is repeated in the description view and the icon // is not individually selectable // Read high temperature from cursor - String high = Utility.formatTemperature( - context, cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP)); - viewHolder.highTempView.setText(high); - viewHolder.highTempView.setContentDescription(context.getString(R.string.a11y_high_temp, high)); + double high = mCursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP); + String highString = Utility.formatTemperature(mContext, high); + viewHolder.mHighTempView.setText(highString); + viewHolder.mHighTempView.setContentDescription(mContext.getString(R.string.a11y_high_temp, highString)); // Read low temperature from cursor - String low = Utility.formatTemperature( - context, cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP)); - viewHolder.lowTempView.setText(low); - viewHolder.lowTempView.setContentDescription(context.getString(R.string.a11y_low_temp, low)); + double low = mCursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP); + String lowString = Utility.formatTemperature(mContext, low); + viewHolder.mLowTempView.setText(lowString); + viewHolder.mLowTempView.setContentDescription(mContext.getString(R.string.a11y_low_temp, lowString)); } public void setUseTodayLayout(boolean useTodayLayout) { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0fe22d6..93505df 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -13,31 +13,10 @@ See the License for the specific language governing permissions and limitations under the License. --> - - - - - - - - \ No newline at end of file