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