78301bd9a69a4be6758c07476077d0d4dbdb6a43
[JavaForFun] /
1 package de.example.exampletdd.fragment.current;
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 WeatherCurrentDataAdapter extends ArrayAdapter<Object> {
13     private static final int FIRST = 0;
14     private static final int SECOND = 1;
15     private static final int THIRD = 2;
16     private final int[] resources;
17
18     public WeatherCurrentDataAdapter(final Context context, final int[] resources) {
19         super(context, 0);
20
21         this.resources = resources;
22     }
23
24
25     @Override
26     public View getView(final int position, final View convertView, final ViewGroup parent) {
27
28         final View view = this.getWorkingView(position, convertView);
29         final int viewType = this.getItemViewType(position);
30
31         if (viewType == FIRST) {
32
33             final ViewFirstHolder viewHolder = this.getViewFirstHolder(view);
34             final WeatherCurrentDataEntryFirst entry = (WeatherCurrentDataEntryFirst) this
35                     .getItem(position);
36             viewHolder.picture.setImageBitmap(entry.getPicture());
37             viewHolder.tempMax.setText(entry.getTempMax());
38             viewHolder.tempMin.setText(entry.getTempMin());
39         } else if (viewType == SECOND) {
40             final ViewSecondHolder viewHolder = this.getViewSecondHolder(view);
41             final WeatherCurrentDataEntrySecond entry = (WeatherCurrentDataEntrySecond) this
42                     .getItem(position);
43             viewHolder.weatherDescription.setText(entry.getWeatherDescription());
44         } else if (viewType == THIRD) {
45             final ViewThirdHolder viewHolder = this.getViewThirdHolder(view);
46             final WeatherCurrentDataEntryFifth entry = (WeatherCurrentDataEntryFifth) this
47                     .getItem(position);
48             viewHolder.humidityValue.setText(entry.getHumidityValue());
49             viewHolder.pressureValue.setText(entry.getPressureValue());
50             viewHolder.rainValue.setText(entry.getRainValue());
51             viewHolder.cloudsValue.setText(entry.getCloudsValue());
52             viewHolder.windValue.setText(entry.getWindValue());
53             viewHolder.sunRiseTime.setText(entry.getSunRiseTime());
54             viewHolder.sunSetTime.setText(entry.getSunSetTime());
55             viewHolder.feelsLike.setText(entry.getFeelsLike());
56             viewHolder.snowValue.setText(entry.getSnowValue());
57             viewHolder.feelsLikeUnits.setText(entry.getFeelsLikeUnits());
58         }
59
60         return view;
61     }
62
63     @Override
64     public int getItemViewType(final int position) {
65         int type = 0;
66
67         if (position == 0) {
68             type = FIRST;
69         } else if (position == 1) {
70             type = SECOND;
71         } else if (position == 2) {
72             type = THIRD;
73         }
74
75         return type;
76     }
77
78     @Override
79     public int getViewTypeCount() {
80         return 3;
81     }
82
83     private View getWorkingView(final int position, final View convertView) {
84         View workingView = null;
85
86         if (convertView == null) {
87             final int viewType = this.getItemViewType(position);
88             final Context context = this.getContext();
89             final LayoutInflater inflater = (LayoutInflater) context
90                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
91
92             workingView = inflater.inflate(this.resources[viewType], null);
93         } else {
94             workingView = convertView;
95         }
96
97         return workingView;
98     }
99
100     private ViewFirstHolder getViewFirstHolder(final View workingView) {
101         final Object tag = workingView.getTag();
102         ViewFirstHolder viewHolder = null;
103
104         if ((null == tag) || !(tag instanceof ViewFirstHolder)) {
105             viewHolder = new ViewFirstHolder();
106
107             viewHolder.picture = (ImageView) workingView
108                     .findViewById(R.id.weather_current_data_picture);
109             viewHolder.tempMax = (TextView) workingView
110                     .findViewById(R.id.weather_current_data_temp_max);
111             viewHolder.tempMin = (TextView) workingView
112                     .findViewById(R.id.weather_current_data_temp_min);
113
114             workingView.setTag(viewHolder);
115
116         } else {
117             viewHolder = (ViewFirstHolder) tag;
118         }
119
120         return viewHolder;
121     }
122
123     private ViewSecondHolder getViewSecondHolder(final View workingView) {
124         final Object tag = workingView.getTag();
125         ViewSecondHolder viewHolder = null;
126
127         if ((null == tag) || !(tag instanceof ViewSecondHolder)) {
128             viewHolder = new ViewSecondHolder();
129
130             viewHolder.weatherDescription = (TextView) workingView
131                     .findViewById(R.id.weather_current_data_description);
132
133             workingView.setTag(viewHolder);
134
135         } else {
136             viewHolder = (ViewSecondHolder) tag;
137         }
138
139         return viewHolder;
140     }
141
142     private ViewThirdHolder getViewThirdHolder(final View workingView) {
143         final Object tag = workingView.getTag();
144         ViewThirdHolder viewHolder = null;
145
146         if ((null == tag) || !(tag instanceof ViewThirdHolder)) {
147             viewHolder = new ViewThirdHolder();
148
149             viewHolder.humidityValue = (TextView) workingView
150                     .findViewById(R.id.weather_current_now_data_humidity_value);
151             viewHolder.pressureValue = (TextView) workingView
152                     .findViewById(R.id.weather_current_now_data_pressure_value);
153             viewHolder.rainValue = (TextView) workingView
154                     .findViewById(R.id.weather_current_now_data_rain_value);
155             viewHolder.cloudsValue = (TextView) workingView
156                     .findViewById(R.id.weather_current_now_data_clouds_value);
157             viewHolder.windValue = (TextView) workingView
158                     .findViewById(R.id.weather_current_now_data_wind_value);
159             viewHolder.cloudsValue = (TextView) workingView
160                     .findViewById(R.id.weather_current_now_data_clouds_value);
161             viewHolder.snowValue = (TextView) workingView
162                     .findViewById(R.id.weather_current_now_data_snow_value);
163             viewHolder.sunRiseTime = (TextView) workingView
164                     .findViewById(R.id.weather_current_now_data_sunrise_value);
165             viewHolder.sunSetTime = (TextView) workingView
166                     .findViewById(R.id.weather_current_now_data_sunset_value);
167             viewHolder.feelsLike = (TextView) workingView
168                     .findViewById(R.id.weather_current_now_data_feelslike_value);
169             viewHolder.feelsLikeUnits = (TextView) workingView
170                     .findViewById(R.id.weather_current_now_data_feelslike_units);
171
172             workingView.setTag(viewHolder);
173
174         } else {
175             viewHolder = (ViewThirdHolder) tag;
176         }
177
178         return viewHolder;
179     }
180
181
182     private static class ViewFirstHolder {
183         public ImageView picture;
184         public TextView tempMax;
185         public TextView tempMin;
186     }
187
188     private static class ViewSecondHolder {
189         public TextView weatherDescription;
190     }
191
192     private static class ViewThirdHolder {
193         public TextView humidityValue;
194         public TextView pressureValue;
195         public TextView windValue;
196         public TextView rainValue;
197         public TextView cloudsValue;
198         public TextView snowValue;
199         public TextView sunRiseTime;
200         public TextView sunSetTime;
201         public TextView feelsLike;
202         public TextView feelsLikeUnits;
203     }
204 }