使用处理程序更新 UI

usinge handler to update UI

我正在尝试编写一个气象应用程序,根据位置提供天气状况,一切似乎都在工作,除了当位置更改时,UI 没有更新,我尝试使用处理程序但我不太清楚如何正确使用它。

这是我使用处理程序的方式:`

private final android.os.Handler handler=new android.os.Handler();
private final Runnable run = new Runnable() {
    @Override
    public void run() {
        displayLocation();

}
};

@Override
public void onLocationChanged(Location location) {

    mLastLocation = location;

    Toast.makeText(getApplicationContext(), "Location changed!",
            Toast.LENGTH_SHORT).show();
    handler.post(run);

}




private void displayLocation() {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    String city = null;
    String country=null;

    if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();
        Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> address = geoCoder.getFromLocation(latitude, longitude, 1);
            city = address.get(0).getLocality();
            country=address.get(0).getCountryName();




            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putString("city",city);
            editor.putString("country", country);
            editor.commit();


        } catch (IOException e) {}
        catch (NullPointerException e) {}
    } else{
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        city =prefs.getString("city", "Austin");
       country=prefs.getString("country","TX");
        service = new YahooWeatherService(this);
        service.refreshWeather(city + ", " + country);
    }
    service = new YahooWeatherService(this);
    service.refreshWeather(city + ", " + country);
}


  @Override
public void serviceSucces(Channel channel) {

    dialog.hide();
    Item item = channel.getItem();
    int resourcesId = getResources().getIdentifier("drawable/icon_" + channel.getItem().getCondition().getCode(),
            null, getPackageName());
    @SuppressWarnings("deprecation")
    Drawable weatherIconDrawable = getResources().getDrawable(resourcesId);

    weatherIconImageView.setImageDrawable(weatherIconDrawable);
    locationTextView.setText(service.getLocation());
    conditionTextView.setText(item.getCondition().getDescription());
    temperatureTextView.setText(item.getCondition().getTemperature() + " \u00B0 " + channel.getUnits().getTemperature());


}

例如,当我 运行 应用程序第一次没有激活位置时,它显示默认位置 "Austin, TX" 但是当我激活位置时 OnlocationChanged() 被触发但是界面不更新

`

试试这个。

  @Override
  public void onLocationChanged(Location location) {

        mLastLocation = location;
        Toast.makeText(getApplicationContext(), "Location changed!",
                Toast.LENGTH_SHORT).show();
        handler.post(new Runnable() {
        @Override
        public void run() {
            displayLocation();
        }
    });
  }