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