ClothesActivity.java
16 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
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 ClothesActivity extends AppCompatActivity {
private Button btn_main;
private Button btn_weather;
public static String start = "";
public static String from = "";
TextView Location;
ClothesRecommend mClothes;
ClothesActivity cThis;
TextView input_Average;
TextView input_Perceived;
TextView input_Wind;
TextView input_Precipiation;
ImageView main_Weather;
ImageView main_Location;
ImageView main_Average;
ImageView main_Perceived;
ImageView main_Wind;
ImageView main_Precipitation;
ImageView today_Cloth;
ImageView recommend;
Bitmap bitmap_Weather;
Bitmap bitmap_Location;
Bitmap bitmap_temperature;
Bitmap bitmap_Wind;
Bitmap bitmap_Precipitation;
Bitmap bitmap_Today;
Bitmap bitmap_Recommend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clothes);
Initialize();
}
public void Initialize(){
cThis = this;
Location = (TextView) findViewById(R.id.txt_location);
btn_main = findViewById(R.id.btn_main);//btn_main 아이디 찾기
btn_main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//버튼 클릭시 WeatherActivity 에서 MainActivity 로 이동
Intent intent = new Intent(ClothesActivity.this, MainActivity.class);
startActivity(intent);// 이동
}
});
Intent intent = getIntent();
String code = intent.getStringExtra("code");
String start = intent.getStringExtra("start");
String from = intent.getStringExtra("from");
String leaf = intent.getStringExtra("leaf");
String gender = intent.getStringExtra("gender");
btn_weather = findViewById(R.id.btn_weather);//btn_weather 아이디 찾기
btn_weather.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//버튼 클릭시 ClothesActivity 에서 WeatherActivity 로 이동 경로
Intent intent = new Intent(ClothesActivity.this, WeatherActivity.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);// 이동
}
});
Location.setText(leaf);
mClothes = new ClothesRecommend((ClothesActivity) cThis);
ImgUpload();
Recommend(start, from, code, gender);
}
public void ImgUpload(){
main_Location = (ImageView)findViewById(R.id.Img_location);
main_Average = (ImageView)findViewById(R.id.Img_aver);
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_Cloth = (ImageView)findViewById(R.id.today_cloth);
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("temperature.png");
bitmap_temperature = BitmapFactory.decodeStream(is);
main_Average.setImageBitmap(bitmap_temperature);
main_Perceived.setImageBitmap(bitmap_temperature);
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("pop.png");
bitmap_Precipitation = BitmapFactory.decodeStream(is);
main_Precipitation.setImageBitmap(bitmap_Precipitation);
is.close();
}catch(Exception e){
e.printStackTrace();
}
try{
is = as.open("today_cloth.png");
bitmap_Today = BitmapFactory.decodeStream(is);
today_Cloth.setImageBitmap(bitmap_Today);
is.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void Recommend(String start, String from, String code, String gender){
ArrayList<ArrayList<String>> gData = new ArrayList<ArrayList<String>>();
input_Average = (TextView)findViewById(R.id.input_aver);
input_Perceived = (TextView)findViewById(R.id.input_perceived);
input_Wind = (TextView)findViewById(R.id.input_wind);
input_Precipiation = (TextView)findViewById(R.id.input_pop);
// 날씨 정보 Get
mClothes = new ClothesRecommend((ClothesActivity) cThis);
gData = mClothes.AccessWeather(code);
try {
int tmp = 0;
int tmx = -50; // 가장 높은 온도
int tmn = 50; // 가장 낮은 온도
int vmx = 0; // 가장 센 풍속
int temp = 0; // 평균 온도를 구하기 위한 temp
int stmp = 0; // 평균 온도를 구하기 위한 sumtmp
int cnt = 0; // 평균 온도를 구하기 위한 cnt
int gen = 0; // 성별
int pop = 0; // 강수확률
int mpop = 0; // 최대 강수확률
ArrayList<Integer> wNum = new ArrayList<>();
// gData에 접근
for (int i = 0; i < gData.size(); i++) {
String hour = gData.get(i).get(0);
System.out.println(start);
System.out.println(from);
if (start.compareTo(hour) <= 0 && from.compareTo(hour) >= 0) {
wNum.add(i);
temp = Integer.parseInt(gData.get(i).get(1));
stmp += temp;
cnt += 1;
tmp = mClothes.Cal_Perceived(gData.get(i).get(1), gData.get(i).get(4));
// 최고 체감온도 설정
if (tmp > tmx) tmx = tmp;
// 최저 체감온도 설정
if (tmp < tmn) tmn = tmp;
double velo = Double.parseDouble(gData.get(i).get(4));
// 최대 풍속 설정
if (vmx < (int) velo)
vmx = (int) velo;
pop = Integer.parseInt(gData.get(i).get(6));
if (pop > mpop) {
mpop = pop;
}
}
}
String main = gData.get(wNum.get(0)).get(5);
main = GetMainWeather(main);
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();
}
if (cnt != 0) stmp = (stmp / cnt);
if (gender.equals("여자")) gen = 1;
int gap = tmx - tmn;
ArrayList<Integer> perceived = new ArrayList<Integer>();
perceived.add(mClothes.Cal_Perceived(String.valueOf(stmp),String.valueOf(vmx)));
perceived.add(gen);
ArrayList<ArrayList<String>> clothes = new ArrayList<ArrayList<String>>();
clothes = mClothes.Recommend(perceived);
ArrayList<String> wCode = clothes.get(0);
ArrayList<String> Top = clothes.get(1);
ArrayList<String> Bottom = clothes.get(2);
// 옷 이미지 정보 불러오기
String mCode = wCode.get(0);
recommend = (ImageView)findViewById(R.id.recommend);
try{
is = as.open(mCode + ".png");
bitmap_Recommend = BitmapFactory.decodeStream(is);
recommend.setImageBitmap(bitmap_Recommend);
is.close();
} catch(Exception e){
e.printStackTrace();
}
input_Average.setText(String.valueOf(stmp) + "°C");
input_Perceived.setText(String.valueOf(tmn) + "°C ~ " + String.valueOf(tmx) + "°C");
input_Wind.setText(String.valueOf(vmx) + "m/s");
input_Precipiation.setText(String.valueOf(mpop) + "%");
int rain;
if (mpop < 60) {
rain = 0;
} else {
rain = 1;
}
GetCheckList(MergeList(Top),MergeList(Bottom), gap, vmx, rain);
} catch(Exception e){
AlertDialog.Builder dlg = new AlertDialog.Builder(ClothesActivity.this);
dlg.setMessage("선택한 시간대에 맞는 날씨를 불러올 수 없어 \n현재 날씨로 대체합니다."); // 메시지
dlg.setPositiveButton("확인",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
});
dlg.show();
ArrayList<String> Now = new ArrayList<>();
Now = gData.get(0);
String main = Now.get(5);
main = GetMainWeather(main);
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();
}
int temp = mClothes.Cal_Perceived(Now.get(1), Now.get(4));
input_Average.setText(Now.get(1) + "°C");
input_Perceived.setText(String.valueOf(temp) + "°C");
input_Wind.setText(Now.get(4) + "m/s");
input_Precipiation.setText(Now.get(6) + "%");
int rain;
if (Integer.parseInt(Now.get(6)) < 60) {
rain = 0;
} else {
rain = 1;
}
int gen = 0;
if (gender.equals("여자"))
gen = 1;
ArrayList<Integer> perceived = new ArrayList<Integer>();
perceived.add(temp);
perceived.add(gen);
ArrayList<ArrayList<String>> clothes = new ArrayList<ArrayList<String>>();
clothes = mClothes.Recommend(perceived);
ArrayList<String> wCode = clothes.get(0);
ArrayList<String> Top = clothes.get(1);
ArrayList<String> Bottom = clothes.get(2);
// 옷 이미지 정보 불러오기
String mCode = wCode.get(0);
recommend = (ImageView)findViewById(R.id.recommend);
try{
is = as.open(mCode + ".png");
bitmap_Recommend = BitmapFactory.decodeStream(is);
recommend.setImageBitmap(bitmap_Recommend);
is.close();
} catch(Exception E){
E.printStackTrace();
}
GetCheckList(MergeList(Top),MergeList(Bottom), 0, Integer.parseInt(Now.get(4)), rain);
}
}
public String MergeList(ArrayList<String> list){
String result = list.get(0);
for (int i = 1; i < list.size(); i++){
result = result + ", " + list.get(i);
}
return result;
}
public String GetMainWeather(String main){
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";
}
return main;
}
public void GetCheckList(String top, String bottom, int gap, int velo, int rain){
ListView listview ;
CheckListAdapter adapter;
// Adapter 생성
adapter = new CheckListAdapter() ;
// 리스트뷰 참조 및 Adapter달기
listview = (ListView) findViewById(R.id.checklist);
listview.setAdapter(adapter);
adapter.addItem(ContextCompat.getDrawable(this, R.drawable.check), "상의는 " + top + "을 추천해요!");
adapter.addItem(ContextCompat.getDrawable(this, R.drawable.check), "하의는 " + bottom + "을 추천해요!");
// 일교차 경고
if (gap >= 10) {
adapter.addItem(ContextCompat.getDrawable(this, R.drawable.check), "일교차가 크니 옷차림에 주의하세요.");
}
// 풍속에 따른 옷 추천
if (velo < 4) {
adapter.addItem(ContextCompat.getDrawable(this, R.drawable.check), "바람이 많이 불지 않아요~");
} else if (velo >= 4 && velo < 9) {
adapter.addItem(ContextCompat.getDrawable(this, R.drawable.check), "바람이 조금 부니 긴팔이나 가디건을 챙겨주세요.");
} else if (velo >= 9 && velo < 14) {
adapter.addItem(ContextCompat.getDrawable(this, R.drawable.check), "바람이 강하니 바람막이나 겉옷을 챙겨주세요.");
} else {
adapter.addItem(ContextCompat.getDrawable(this, R.drawable.check), "아주 강한 바람이므로 외출을 자제해주세요.");
}
// 강수 확률에 따른 옷 추천
if (rain == 0){
adapter.addItem(ContextCompat.getDrawable(this, R.drawable.check), "오늘은 비소식이 없네요!");
} else if (rain == 1){
adapter.addItem(ContextCompat.getDrawable(this, R.drawable.check), "오늘 비소식이 있으니 우산이나 우비를 챙기세요!!");
}
setListViewHeightBasedOnChildren(listview);
}
// 리스트뷰 전체 목록 보이는 함수
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();
}
}