android - usinge handler to update UI -


im trying code meteo application gives weather condition based on location, seems working except when location change, ui doesn't update , tried use handler don't quite know how use it.

this how used handler : `

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());   } 

for example when run application first time without position activated shows default location "austin, tx" when activate position onlocationchanged()get ttriggered interface doesn't update

`

try this.

  @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();         }     });   } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -