2 * Copyright 2014 Gustavo Martin Morcuende
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package name.gumartinm.weather.information.fragment.overview;
18 import android.content.Context;
19 import android.view.LayoutInflater;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.widget.ArrayAdapter;
23 import android.widget.ImageView;
24 import android.widget.TextView;
26 import name.gumartinm.weather.information.R;
28 public class OverviewAdapter extends ArrayAdapter<OverviewEntry> {
29 private final int resource;
31 public OverviewAdapter(final Context context, final int resource) {
34 this.resource = resource;
38 public View getView(final int position, final View convertView,
39 final ViewGroup parent) {
41 // We need to get the best view (re-used if possible) and then
42 // retrieve its corresponding ViewHolder, which optimizes lookup
44 final View view = this.getWorkingView(convertView);
45 final ViewHolder viewHolder = this.getViewHolder(view);
46 final OverviewEntry entry = this.getItem(position);
50 viewHolder.dateNameView.setText(entry.getDateName());
51 viewHolder.dateNumberView.setText(entry.getDateNumber());
53 // Setting temperature max/min
54 viewHolder.temperatureMaxView.setText(entry.getMaxTemp());
55 viewHolder.temperatureMinView.setText(entry.getMinTemp());
58 viewHolder.pictureView.setImageBitmap(entry.getPicture());
64 private View getWorkingView(final View convertView) {
65 // The workingView is basically just the convertView re-used if possible
66 // or inflated new if not possible
67 View workingView = null;
69 if(null == convertView) {
70 final Context context = this.getContext();
71 final LayoutInflater inflater = (LayoutInflater)context.getSystemService
72 (Context.LAYOUT_INFLATER_SERVICE);
74 workingView = inflater.inflate(this.resource, null);
76 workingView = convertView;
82 private ViewHolder getViewHolder(final View workingView) {
83 // The viewHolder allows us to avoid re-looking up view references
84 // Since views are recycled, these references will never change
85 final Object tag = workingView.getTag();
86 ViewHolder viewHolder = null;
89 if((null == tag) || !(tag instanceof ViewHolder)) {
90 viewHolder = new ViewHolder();
92 viewHolder.dateNameView = (TextView) workingView
93 .findViewById(R.id.weather_main_entry_date_name);
94 viewHolder.dateNumberView = (TextView) workingView
95 .findViewById(R.id.weather_main_entry_date_number);
96 viewHolder.temperatureMaxView = (TextView) workingView
97 .findViewById(R.id.weather_main_entry_temperature_max);
98 viewHolder.temperatureMinView = (TextView) workingView
99 .findViewById(R.id.weather_main_entry_temperature_min);
100 viewHolder.pictureView = (ImageView) workingView
101 .findViewById(R.id.weather_main_entry_image);
103 workingView.setTag(viewHolder);
106 viewHolder = (ViewHolder) tag;
113 * ViewHolder allows us to avoid re-looking up view references
114 * Since views are recycled, these references will never change
116 private static class ViewHolder {
117 public TextView dateNameView;
118 public TextView dateNumberView;
119 public TextView temperatureMaxView;
120 public TextView temperatureMinView;
121 public ImageView pictureView;