392faa7d300668126c22ba37dc3f7aa7b9d3ca71
[JavaForFun] /
1 package com.weather.information.fragment.overview;
2
3 import android.content.Context;
4 import android.view.LayoutInflater;
5 import android.view.View;
6 import android.view.ViewGroup;
7 import android.widget.ArrayAdapter;
8 import android.widget.ImageView;
9 import android.widget.TextView;
10
11 import com.weather.information.R;
12
13 public class OverviewAdapter extends ArrayAdapter<OverviewEntry> {
14     private final int resource;
15
16     public OverviewAdapter(final Context context, final int resource) {
17         super(context, 0);
18
19         this.resource = resource;
20     }
21
22     @Override
23     public View getView(final int position, final View convertView,
24             final ViewGroup parent) {
25
26         // We need to get the best view (re-used if possible) and then
27         // retrieve its corresponding ViewHolder, which optimizes lookup
28         // efficiency
29         final View view = this.getWorkingView(convertView);
30         final ViewHolder viewHolder = this.getViewHolder(view);
31         final OverviewEntry entry = this.getItem(position);
32
33
34         // Setting date
35         viewHolder.dateNameView.setText(entry.getDateName());
36         viewHolder.dateNumberView.setText(entry.getDateNumber());
37
38         // Setting temperature max/min
39         viewHolder.temperatureMaxView.setText(entry.getMaxTemp());
40         viewHolder.temperatureMinView.setText(entry.getMinTemp());
41
42         // Set image view
43         viewHolder.pictureView.setImageBitmap(entry.getPicture());
44
45
46         return view;
47     }
48
49     private View getWorkingView(final View convertView) {
50         // The workingView is basically just the convertView re-used if possible
51         // or inflated new if not possible
52         View workingView = null;
53
54         if(null == convertView) {
55             final Context context = this.getContext();
56             final LayoutInflater inflater = (LayoutInflater)context.getSystemService
57                     (Context.LAYOUT_INFLATER_SERVICE);
58
59             workingView = inflater.inflate(this.resource, null);
60         } else {
61             workingView = convertView;
62         }
63
64         return workingView;
65     }
66
67     private ViewHolder getViewHolder(final View workingView) {
68         // The viewHolder allows us to avoid re-looking up view references
69         // Since views are recycled, these references will never change
70         final Object tag = workingView.getTag();
71         ViewHolder viewHolder = null;
72
73
74         if((null == tag) || !(tag instanceof ViewHolder)) {
75             viewHolder = new ViewHolder();
76
77             viewHolder.dateNameView = (TextView) workingView
78                     .findViewById(R.id.weather_main_entry_date_name);
79             viewHolder.dateNumberView = (TextView) workingView
80                     .findViewById(R.id.weather_main_entry_date_number);
81             viewHolder.temperatureMaxView = (TextView) workingView
82                     .findViewById(R.id.weather_main_entry_temperature_max);
83             viewHolder.temperatureMinView = (TextView) workingView
84                     .findViewById(R.id.weather_main_entry_temperature_min);
85             viewHolder.pictureView = (ImageView) workingView
86                     .findViewById(R.id.weather_main_entry_image);
87
88             workingView.setTag(viewHolder);
89
90         } else {
91             viewHolder = (ViewHolder) tag;
92         }
93
94         return viewHolder;
95     }
96
97     /**
98      * ViewHolder allows us to avoid re-looking up view references
99      * Since views are recycled, these references will never change
100      */
101     private static class ViewHolder {
102         public TextView dateNameView;
103         public TextView dateNumberView;
104         public TextView temperatureMaxView;
105         public TextView temperatureMinView;
106         public ImageView pictureView;
107     }
108
109 }