Android簡單版天氣預報源碼,整體重構(第四步)

Android簡單版天氣預報源碼,整體重構(第四步)

找到一個比較好用的天氣數據API

http://wthrcdn.etouch.cn/weather_mini?city=深圳
http://wthrcdn.etouch.cn/weather_mini?citykey=101280601

  • 這兩個都可以 我選擇了第一個進行更新,不用之前的的心知天氣API了

快春節了,更新一下,說是第四步,也不是哈哈哈哈

項目源碼地址
https://github.com/LGH-cmd/weather-report

前端UI跟之前差不多

在這裏插入圖片描述
在這裏插入圖片描述

改動點1

改動了API,當然要重新寫GSON

也就依葫蘆畫瓢,就不展示了
MainActivity 大部分實現也就是得到數據後展示到頁面

整體代碼

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private TextView tvTitleName; // 城市名稱
    private TextView tvNowDate; // 今天的日期
    private TextView tvNowTemp; // 今天的溫度
    private TextView tvNowType; // 今天的天氣類型
    private TextView tvNowWind; // 今天的風向
    private TextView tvNowGanmao; // 今天的提示
    private Button SwitchCity; // 切換其他城市
    private ListView lvForecast ; // 預報ListView
    private List<ForecastData> dataList = new ArrayList<>(); // 預報列表
    private ForecastAdapter adapter; // 預報適配器
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvTitleName = findViewById(R.id.tv_title_name);
        tvNowDate = findViewById(R.id.tv_now_date);
        tvNowTemp = findViewById(R.id.tv_now_temp);
        tvNowType = findViewById(R.id.tv_now_type);
        tvNowWind = findViewById(R.id.tv_now_wind);
        tvNowGanmao = findViewById(R.id.tv_now_ganmao);
        SwitchCity = findViewById(R.id.btn_switch_city);
        lvForecast = findViewById(R.id.lv_forecast_content);
        GetDataAndShowData("深圳");

        // 選擇其他城市
        SwitchCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SelectCityActivity.class);
                startActivityForResult(intent, 1001);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == 1001 && resultCode == 1002) {
            String cityName = data.getStringExtra("cityName");
            GetDataAndShowData(cityName);
        }
    }

    private void GetDataAndShowData(String cityName) {
        ProgressDialogUtil.showProgressDialog(MainActivity.this); // 加載框
        String address = "http://wthrcdn.etouch.cn/weather_mini?city="+cityName;
        DataUtil.sendOkHttpRequest(address, new Callback() {
            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            WeatherResponse weatherData = DataUtil.getWeatherData(response.body().string());
                            String cityName = weatherData.getData().getCity(); // 城市名稱
                            tvTitleName.setText(cityName);
                            String wendu = weatherData.getData().getWendu(); // 今天的溫度
                            tvNowTemp.setText(wendu+"℃");
                            String ganmao = weatherData.getData().getGanmao(); // 今天的提示
                            tvNowGanmao.setText(ganmao);
                            List<Forecast> forecast = weatherData.getData().getForecast(); // 天氣信息
                            ForecastData nowData = setNowWeatherData(forecast); // 今天的天氣信息

                            // 設置其他日期的信息
                            // 昨天
                            Yesterday yesterday = weatherData.getData().getYesterday();
                            ForecastData yesterdayData = setYesterdayWeatherData(yesterday);
                            // 未來幾天
                            ForecastData futureOne = setOtherDayWeatherData(forecast.get(1));
                            ForecastData futureTwo = setOtherDayWeatherData(forecast.get(2));
                            ForecastData futureThree = setOtherDayWeatherData(forecast.get(3));
                            ForecastData futureFour = setOtherDayWeatherData(forecast.get(4));
                            if(dataList.size()!=0) {
                                dataList.clear(); // 清除請一次的數據
                            }
                            dataList.add(yesterdayData);
                            dataList.add(nowData);
                            dataList.add(futureOne);dataList.add(futureTwo);dataList.add(futureThree);dataList.add(futureFour);
                            adapter = new ForecastAdapter(dataList);
                            lvForecast.setAdapter(adapter);

                            ProgressDialogUtil.dismiss(); // 消失加載框
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });

            }
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "獲得天氣出錯了" + e);
            }
        });
    }

    /**
     * 設置今天的天氣信息,返回一個ForecastData,用於在預告中顯示
     * @param forecast
     * @return
     */
    private ForecastData setNowWeatherData(List<Forecast> forecast) {
        Forecast forecastNow = forecast.get(0); // 今天的天氣信息
        String nowDate = forecastNow.getDate(); // 今天的日期
        tvNowDate.setText("今天-"+nowDate);
        String nowTemp = forecastNow.getHigh(); // 今天的最高溫度
        String nowLow = forecastNow.getLow(); // 今天的最低溫度
        String nowType = forecastNow.getType(); // 今天的天氣類型
        tvNowType.setText(nowType);
        String nowWind = forecastNow.getFengxiang(); // 今天的風向
        tvNowWind.setText(nowWind);

        ForecastData nowData = new ForecastData(nowDate, nowTemp.substring(2), nowLow.substring(2), nowType); // 今天的天氣
        return nowData;
    }

    /**
     * 設置昨天的信息
     * @param yesterday
     * @return
     */
    private ForecastData setYesterdayWeatherData(Yesterday yesterday) {
        String date = yesterday.getDate(); // 昨天的日期
        String high = yesterday.getHigh(); // 昨天的最高溫度
        String low = yesterday.getLow(); // 昨天的最低溫度
        String type = yesterday.getType(); // 昨天的天氣類型
        ForecastData yesterdayData = new ForecastData(date, high.substring(2), low.substring(2), type); // 昨天的天氣
        return yesterdayData;
    }

    /**
     * 未來幾天的天氣
     * @param forecast
     * @return
     */
    private ForecastData setOtherDayWeatherData(Forecast forecast) {
        String date = forecast.getDate(); // 未來幾天的日期
        String high = forecast.getHigh(); // 未來幾天的最高溫度
        String low = forecast.getLow(); // 未來幾天的最低溫度
        String type = forecast.getType(); // 未來幾天的天氣類型
        ForecastData otherDayData = new ForecastData(date, high.substring(2), low.substring(2), type); // 未來幾天的天氣
        return otherDayData;
    }

}

不同點,在預報中用的是ListView展示
也不是很難實現。與之前有相同之處,可參考前面,完整代碼已經在GitHub上

改動點2

將可以選的城市擴充了

在strings.xml寫入城市數組
在這裏插入圖片描述

<string-array name="city_list">
        <item>廣州</item>
        <item>番禺</item>
        <item>從化</item>
        <item>增城</item>
        <item>花都</item>
        <item>韶關</item>
        <item>乳源</item>
        <item>始興</item>
        <item>翁源</item>
        <item>樂昌</item>
        <item>仁化</item>
        <item>南雄</item>
        <item>新豐</item>
        <item>曲江</item>
        <item>湞江</item>
        <item>武江</item>
        <item>惠州</item>
        <item>博羅</item>
        <item>惠陽</item>
        <item>惠東</item>
        <item>龍門</item>
        <item>梅州</item>
        <item>興寧</item>
        <item>蕉嶺</item>
        <item>大埔</item>
        <item>豐順</item>
        <item>平遠</item>
        <item>五華</item>
        <item>梅縣</item>
        <item>汕頭</item>
        <item>潮陽</item>
        <item>澄海</item>
        <item>南澳</item>
        <item>深圳</item>
        <item>珠海</item>
        <item>斗門</item>
        <item>金灣</item>
        <item>佛山</item>
        <item>順德</item>
        <item>三水</item>
        <item>南海</item>
        <item>高明</item>
        <item>肇慶</item>
        <item>廣寧</item>
        <item>四會</item>
        <item>德慶</item>
        <item>懷集</item>
        <item>封開</item>
        <item>高要</item>
        <item>湛江</item>
        <item>吳川</item>
        <item>雷州</item>
        <item>徐聞</item>
        <item>廉江</item>
        <item>赤坎</item>
        <item>遂溪</item>
        <item>坡頭</item>
        <item>霞山</item>
        <item>麻章</item>
        <item>江門</item>
        <item>開平</item>
        <item>新會</item>
        <item>恩平</item>
        <item>台山</item>
        <item>蓬江</item>
        <item>鶴山</item>
        <item>江海</item>
        <item>河源</item>
        <item>紫金</item>
        <item>連平</item>
        <item>和平</item>
        <item>龍川</item>
        <item>東源</item>
        <item>清遠</item>
        <item>連南</item>
        <item>連州</item>
        <item>連山</item>
        <item>陽山</item>
        <item>佛岡</item>
        <item>英德</item>
        <item>清新</item>
        <item>雲浮</item>
        <item>羅定</item>
        <item>新興</item>
        <item>鬱南</item>
        <item>雲安</item>
        <item>潮州</item>
        <item>饒平</item>
        <item>潮安</item>
        <item>東莞</item>
        <item>中山</item>
        <item>陽江</item>
        <item>陽春</item>
        <item>陽東</item>
        <item>陽西</item>
        <item>揭陽</item>
        <item>揭西</item>
        <item>普寧</item>
        <item>惠來</item>
        <item>揭東</item>
        <item>茂名</item>
        <item>高州</item>
        <item>化州</item>
        <item>電白</item>
        <item>信宜</item>
        <item>茂港</item>
        <item>汕尾</item>
        <item>海豐</item>
        <item>陸豐</item>
        <item>陸河</item>
    </string-array>

SelectCityActivity實現列表展示

public class SelectCityActivity extends AppCompatActivity {
    private ListView lvCityList;
    private ArrayAdapter<String> adapter;
    private String[] cityList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_city);
        lvCityList = findViewById(R.id.lv_city_list);
        cityList = getResources().getStringArray(R.array.city_list);
        adapter=new ArrayAdapter<>(SelectCityActivity.this,android.R.layout.simple_list_item_1,cityList);
        lvCityList.setAdapter(adapter);

        lvCityList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String cityName = cityList[i];
                Toast.makeText(SelectCityActivity.this,cityName,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("cityName",cityName);
                setResult(1002, intent);
                finish();
            }
        });

    }
}

更新了 比之前好多了 後面繼續實現其他功能

發佈了20 篇原創文章 · 獲贊 6 · 訪問量 4903
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章