酷欧天气bug

    xiaoxiao2021-03-25  118

    源码地址

    本文出自EzraZhao,转载请注明出处。

    本篇文章只适合刚入门菜鸟,大神级请绕道,谢谢。

    废话不多说,相信大家有不少人都在学习或者学过郭霖郭大神的第一行代码,本人在学习第二版时,在制作酷欧天气中发现一个小bug,不知道大家有没有发现。

    在你启动郭神的应用后,现在假如已经加载了一个城市的天气信息,然后你进行切换城市操作,然后进行刷新操作,哎,你会发现城市竟然又切回了启动时的城市天气,而且不管你切换几次,只要一刷新,就会切回原来的。

    这个小bug在我做完随意切换城市时发现的。然后进行一番调试,发现问题的根源在于WeatherActivity的onCreate()方法

    final String weatherId;

    if (weatherString != null) { //有缓存时直接解析天气数据 Weather weather = Utility.handleWeatherResponse(weatherString); weatherId = weather.basic.weatherId; showWeatherInfo(weather); } else { //无缓存时去服务器查询天气 weatherId = getIntent().getStringExtra(“weather_id”); //请求数据时先将ScrollView隐藏,否则空界面看上去会比较奇怪 weatherLayout.setVisibility(View.INVISIBLE); requestWeather(weatherId); } 不知道你发现问题没,如果没有发现,好,让我们一起来回想一下Activity的生命周期,还记得onCreate()方法只会在Activity创建时执行一次吗?明白没,对了,就是这个问题,咱们的weatherId只会在WeatherActivity创建时被加载一次,也就是说当你启动应用后,咱们的应用会默认加载一次天气,如果你是第一次安装,那么就会从服务器加载,没问题,如果你是已经打开过,会从缓存加载,没问题。然后你切换一次城市,哎,问题来了,由于咱们的WeatherActivity创建完了,然后weatherId就不会再更改了,所以咱们的

    swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { requestWeather(weatherId); } }); 这个方法每次传入的id都是在Activity创建时的同一个,一刷新当然会改回去咯。

    然而这个小bug怎么解决呢?so easy,weatherId不是不变么,来,咱们来解决一下:

    先提供一种简单的方案: 哦,requestWeather(String weatherId)这个这个里面的id每次都是新的,那咱们就把weatherId从onCreate()方法中拿出去,变成全局变量,然后在requestWeather(String weatherId)这个方法中更新一下,

    public void requestWeather(String weatherId) { String weatherUrl = “http://guolin.tech/api/weather?cityid=” + weatherId + “&key=” + API_KEY; //使weatherId成为全局变量,在每一次请求时都更新当前id this.weatherId = weatherId; … } 就是这么简单,每次更新一下,解决!

    咱们再来看一种不把weatherId变成全局变量的方法:

    这个方法借助SharedPreferences将weatherId在选择城市后进行本地存储一下,来,我们改一下ChooseAreaFragment这个类中的代码:

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (currentLevel == LEVEL_PROVINCE) { selectedProvince = provinceList.get(position); queryCities(); } else if (currentLevel == LEVEL_CITY) { selectedCity = cityList.get(position); queryCounties(); } else if (currentLevel == LEVEL_COUNTY) { String weatherId = countyList.get(position).getWeatherId(); if (getActivity() instanceof MainActivity) { Intent intent = new Intent(getActivity(), WeatherActivity.class); intent.putExtra("weather_id", weatherId); startActivity(intent); getActivity().finish(); } else if (getActivity() instanceof WeatherActivity) { WeatherActivity activity = (WeatherActivity) getActivity(); //对,改的就是这! SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(WeatherActivity.this) .edit(); editor.putString("weather_id", weatherId); editor.apply(); activity.drawerLayout.closeDrawers(); activity.swipeRefresh.setRefreshing(true); activity.requestWeather(weatherId); } } } });

    然而这样完了吗?当然并没有,我们只是存储了,在WeatherActivity中还是只读取了一次啊,下面我们再来改一下刷新时监听器代码:

    swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(WeatherActivity.this); String temp = prefs.getString(“weather_id”, weatherId); requestWeather(temp); } }); 是不是也是so easy, 我们跑一下吧~ (@ο@) 哇哈哈哈~改完bug就是爽!

    欢迎交流。

    转载请注明原文地址: https://ju.6miu.com/read-8129.html

    最新回复(0)