WeatherActivity.java 11.7 KB
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();
    }
}