Java tutorial
/* * $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ * $ $ * $ _oo0oo_ $ * $ o8888888o $ * $ 88" . "88 $ * $ (| -_- |) $ * $ 0\ = /0 $ * $ ___/`-_-'\___ $ * $ .' \\| |$ '. $ * $ / \\||| : |||$ \ $ * $ / _||||| -:- |||||- \ $ * $ | | \\\ - $/ | | $ * $ | \_| ''\- -/'' |_/ | $ * $ \ .-\__ '-' ___/-. / $ * $ ___'. .' /-_._-\ `. .'___ $ * $ ."" '< `.___\_<|>_/___.' >' "". $ * $ | | : `- \`.;`\ _ /`;.`/ - ` : | | $ * $ \ \ `_. \_ __\ /__ _/ .-` / / $ * $ =====`-.____`.___ \_____/___.-`___.-'===== $ * $ `=-_-=' $ * $ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ $ * $ $ * $ Buddha Bless Never Bug $ * $ $ * $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ * * Copyright (C) 2016 The Android Open Source Project * * 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.jackie.sunshine.app; import android.content.Context; import android.database.Cursor; import android.support.annotation.DrawableRes; import android.support.v4.widget.CursorAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; /** * Created 16/12/12. * * @author Jackie * @version 1.0 */ public class ForecastAdapter extends CursorAdapter { private final int VIEW_TYPE_COUNT = 2; private final int VIEW_TYPE_TODAY = 0; private final int VIEW_TYPE_FUTURE_DAY = 1; private boolean userTodayLayout = true; public static class ViewHolder { public final ImageView iconView; public final TextView dateView; public final TextView descriptionView; public final TextView highView; public final TextView lowView; 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); highView = (TextView) view.findViewById(R.id.list_item_high_textview); lowView = (TextView) view.findViewById(R.id.list_item_low_textview); } } public ForecastAdapter(Context context, Cursor c, int flags) { super(context, c, flags); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { int viewType = getItemViewType(cursor.getPosition()); int layoutId; if (viewType == VIEW_TYPE_TODAY) { layoutId = R.layout.list_item_forecast_today; } else { layoutId = R.layout.list_item_forecast; } View view = LayoutInflater.from(context).inflate(layoutId, parent, false); ViewHolder viewHolder = new ViewHolder(view); view.setTag(viewHolder); return view; } @Override public void bindView(View view, Context context, Cursor cursor) { ViewHolder viewHolder = (ViewHolder) view.getTag(); // Read weather icon ID from cursor int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_CONDITION_ID); // Use placeholder image for now @DrawableRes int iconRes = R.drawable.art_clear; switch (getItemViewType(cursor.getPosition())) { case VIEW_TYPE_TODAY: iconRes = Utility.getArtResourceForWeatherCondition(weatherId); break; case VIEW_TYPE_FUTURE_DAY: iconRes = Utility.getIconResourceForWeatherCondition(weatherId); break; } viewHolder.iconView.setImageResource(iconRes); // Read date from cursor long dataInMillis = cursor.getLong(ForecastFragment.COL_WEATHER_DATE); // Find TextView and set formatted date on it viewHolder.dateView.setText(Utility.getFriendlyDayString(context, dataInMillis)); // Read weather forecast from cursor String description = cursor.getString(ForecastFragment.COL_WEATHER_DESC); // Find TextView and set weather forecast on it viewHolder.descriptionView.setText(description); viewHolder.iconView.setContentDescription(description); // Read user preference for metric or imperial temperature units boolean isMetric = Utility.isMetric(context); // Read high temperature from cursor double high = cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP); viewHolder.highView.setText(Utility.formatTemperature(context, high)); // Read low temperature from cursor double low = cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP); viewHolder.lowView.setText(Utility.formatTemperature(context, low)); } public void setUserTodayLayout(boolean userTodayLayout) { this.userTodayLayout = userTodayLayout; } @Override public int getItemViewType(int position) { return (position == 0 && userTodayLayout) ? VIEW_TYPE_TODAY : VIEW_TYPE_FUTURE_DAY; } @Override public int getViewTypeCount() { return VIEW_TYPE_COUNT; } }