WeatherActivity.java
11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.io.InputStream;
import java.util.ArrayList;
public class WeatherActivity extends AppCompatActivity {
private Button btn_main;
private Button btn_clothes;
TextView Location;
WeatherForCast mForeCast;
WeatherActivity wThis;
ImageView main_Weather;
ImageView main_Location;
ImageView main_Average;
ImageView main_MinMax;
ImageView main_Perceived;
ImageView main_Wind;
ImageView main_Precipitation;
ImageView today_Weather;
ImageView weather_Blank;
Bitmap bitmap_Weather;
Bitmap bitmap_Location;
Bitmap bitmap_temperature;
Bitmap bitmap_wind;
Bitmap bitmap_Precipitation;
Bitmap bitmap_Today;
Bitmap bitmap_Blank;
TextView Now;
TextView Perceived;
TextView Wind;
TextView Precipitation;
TextView Select;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
Initialize();
}
public void Initialize(){
wThis = this;
Location = (TextView) findViewById(R.id.txt_location);
Intent intent = getIntent();
String code = intent.getStringExtra("code");
mForeCast = new WeatherForCast(code, (WeatherActivity) wThis);
String start = intent.getStringExtra("start");
String from = intent.getStringExtra("from");
String leaf = intent.getStringExtra("leaf");
String gender = intent.getStringExtra("gender");
Location.setText(leaf);
main_Location = (ImageView)findViewById(R.id.Img_location);
AssetManager as = getResources().getAssets();
InputStream is = null;
try{
is = as.open("location.png");
bitmap_Location = BitmapFactory.decodeStream(is);
main_Location.setImageBitmap(bitmap_Location);
is.close();
}catch(Exception e){
e.printStackTrace();
}
ImgUpload();
GetWeather(start, from, code);
btn_main = findViewById(R.id.btn_main);//btn_first 아이디을 찾아라
btn_main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//버튼 클릭시 SecondActivity 에서 MainActivity 로 이동 경로
Intent intent = new Intent(WeatherActivity.this, MainActivity.class);
startActivity(intent);// 이동
}
});
btn_clothes = findViewById(R.id.btn_clothes);//btn_clothes 아이디 찾기
btn_clothes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//버튼 클릭시 WeatherActivity 에서 ClothesActivity 로 이동 경로
Intent intent = new Intent(WeatherActivity.this, ClothesActivity.class);
intent.putExtra("code", code); //code에 있는 값 이동
intent.putExtra("start", start); //start에 있는 값 이동
intent.putExtra("from", from); //from에 있는 값 이동
intent.putExtra("leaf", leaf); //leaf에 있는 값 이동
intent.putExtra("gender", gender); //gender에 있는 값 이동
startActivity(intent);// 이동
}
});
}
public void ImgUpload(){
main_Location = (ImageView)findViewById(R.id.Img_location);
main_Average = (ImageView)findViewById(R.id.Img_aver);
main_MinMax = (ImageView)findViewById(R.id.Img_minmax);
main_Perceived = (ImageView)findViewById(R.id.Img_perceived);
main_Wind = (ImageView)findViewById(R.id.Img_wind);
main_Precipitation = (ImageView)findViewById(R.id.Img_pop);
today_Weather = (ImageView)findViewById(R.id.today_weather);
AssetManager as = getResources().getAssets();
InputStream is = null;
try{
is = as.open("location.png");
bitmap_Location = BitmapFactory.decodeStream(is);
main_Location.setImageBitmap(bitmap_Location);
is.close();
}catch(Exception e){
e.printStackTrace();
}
try{
is = as.open("wind.png");
bitmap_wind = BitmapFactory.decodeStream(is);
main_Wind.setImageBitmap(bitmap_wind);
is.close();
}catch(Exception e){
e.printStackTrace();
}
try{
is = as.open("temperature.png");
bitmap_temperature = BitmapFactory.decodeStream(is);
main_Average.setImageBitmap(bitmap_temperature);
main_MinMax.setImageBitmap(bitmap_temperature);
is.close();
}catch(Exception e){
e.printStackTrace();
}
try{
is = as.open("pop.png");
bitmap_Precipitation = BitmapFactory.decodeStream(is);
main_Precipitation.setImageBitmap(bitmap_Precipitation);
is.close();
}catch(Exception e){
e.printStackTrace();
}
try{
is = as.open("today_weather.png");
bitmap_Today = BitmapFactory.decodeStream(is);
today_Weather.setImageBitmap(bitmap_Today);
is.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void GetWeather(String start, String from, String code){
ArrayList<ArrayList<String>> wData = new ArrayList<ArrayList<String>>();
wData = mForeCast.GetOpenWeather(code);
System.out.println("Start GetWeather");
System.out.println(wData);
String mData = "";
int sMonth = Integer.parseInt(start.substring(0,2));
int sDay = Integer.parseInt(start.substring(2,4));
int sHour = Integer.parseInt(start.substring(4,6));
int fMonth = Integer.parseInt(from.substring(0,2));
int fDay = Integer.parseInt(from.substring(2,4));
int fHour = Integer.parseInt(from.substring(4,6));
String select = String.valueOf(sMonth) + "월 " + String.valueOf(sDay) + "일 " + String.valueOf(sHour) + "시부터 "
+ String.valueOf(fMonth) + "월 " + String.valueOf(fDay) + "일 " + String.valueOf(fHour) + "시까지";
// TextView 연결
Now = (TextView)findViewById(R.id.input_now);
Perceived = (TextView)findViewById(R.id.input_perceived);
Wind = (TextView)findViewById(R.id.input_wind);
Precipitation = (TextView)findViewById(R.id.input_pop);
Select = (TextView)findViewById(R.id.select_time);
// 현재 날씨 input
Now.setText(wData.get(0).get(1) + "°C");
Perceived.setText(mForeCast.GetPerceived(wData.get(0).get(1), wData.get(0).get(4)) + "°C");
Wind.setText(wData.get(0).get(4) + "m/s");
Precipitation.setText(wData.get(0).get(6) + "%");
Select.setText(select);
ListView listview ;
ListViewAdapter adapter;
// Adapter 생성
adapter = new ListViewAdapter() ;
// 리스트뷰 참조 및 Adapter달기
listview = (ListView) findViewById(R.id.listview1);
listview.setAdapter(adapter);
// 날씨에 맞는 id값 받아오기
int clear = R.drawable.clear;
int cloudy = R.drawable.cloudy;
int mostcloudy = R.drawable.mostcloudy;
int partcloudy = R.drawable.partcloudy;
int rain = R.drawable.rain;
int snow = R.drawable.snow;
int snowrain = R.drawable.snowrain;
int weather = clear;
String main = wData.get(0).get(5);
if (main.equals("Clear")){
main = "wClear";
} else if (main.equals("Partly Cloudy")){
main = "wPartcloudy";
} else if (main.equals("Mostly Cloudy")){
main = "wMostcloudy";
} else if (main.equals("Cloudy")){
main = "wCloudy";
} else if (main.equals("Rain")){
main = "wRain";
} else if (main.equals("Snow/Rain")){
main = "wSnowrain";
} else if (main.equals("Snow")){
main = "wSnow";
}
AssetManager as = getResources().getAssets();
InputStream is = null;
main_Weather = (ImageView)findViewById(R.id.main_weather);
try{
is = as.open(main + ".png");
bitmap_Weather = BitmapFactory.decodeStream(is);
main_Weather.setImageBitmap(bitmap_Weather);
is.close();
}catch(Exception e){
e.printStackTrace();
}
for (int i = 0; i < wData.size(); i++){
String hour = wData.get(i).get(0);
String time = "";
time = hour.substring(4,6);
int num = Integer.parseInt(time);
if (num <= 12){
time = "오전 " + String.valueOf(num) + "시";
} else {
time = "오후 " + String.valueOf(num) + "시";
}
if (start.compareTo(hour) <= 0 && from.compareTo(hour) >= 0){
if (wData.get(i).get(5).equals("Clear")){
weather = clear;
} else if (wData.get(i).get(5).equals("Partly Cloudy")){
weather = partcloudy;
} else if (wData.get(i).get(5).equals("Mostly Cloudy")){
weather = mostcloudy;
} else if (wData.get(i).get(5).equals("Cloudy")){
weather = cloudy;
} else if (wData.get(i).get(5).equals("Rain")){
weather = rain;
} else if (wData.get(i).get(5).equals("Snow/Rain")){
weather = snowrain;
} else if (wData.get(i).get(5).equals("Snow")){
weather = snow;
}
adapter.addItem(time,ContextCompat.getDrawable(this, weather),
wData.get(i).get(1) + "°C", wData.get(i).get(4) + "m/s", wData.get(i).get(6) + "%") ;
}
}
setListViewHeightBasedOnChildren(listview);
if (adapter.getCount() == 0){
weather_Blank = (ImageView)findViewById(R.id.Img_blank);
try{
is = as.open("blank.png");
bitmap_Blank = BitmapFactory.decodeStream(is);
weather_Blank.setImageBitmap(bitmap_Blank);
is.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
// 리스트뷰 전체 목록 보이는 함수
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}