android - How to display image based on weather from drawable -


i display image based on weather. image i'm getting from drawable.

for example : if code = 30, display rainy day icon

i'm retrieving from

   <yweather:condition  text="light rain thunder"  code="4"  temp="25"  date="thu, 18 jul 2013 7:58 sgt" /> 

here code

mainactivity.java

public class mainactivity extends activity {   textview weather;  imageview image;   class myweather{     string conditiontext;   string conditiondate;    string numberofforecast;   string forecast;    public string tostring(){     return "\n- "             + "weather:" + image + "\n"      + "condition: " + conditiontext + "\n"     + conditiondate +"\n"      + "\n"     + "number of forecast: " + numberofforecast + "\n"     + forecast;    }   }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         weather = (textview)findviewbyid(r.id.weather);         image = (imageview)findviewbyid(r.id.image);          thread mythread = new thread(new runnable(){     @override    public void run() {     string weatherstring = queryyahooweather();           document weatherdoc = convertstringtodocument(weatherstring);            final myweather weatherresult = parseweather(weatherdoc);           runonuithread(new runnable(){       @override      public void run() {       weather.settext(weatherresult.tostring());      }});     }});         mythread.start();     }      private myweather parseweather(document srcdoc){       myweather myweather = new myweather();          //<yweather:condition.../>      node conditionnode = srcdoc.getelementsbytagname("yweather:condition").item(0);      if(conditionnode.gettextcontent().equals("11")){          image.setimageresource(r.drawable.logo);      }      else {       image.setimageresource(r.drawable.old);      }      myweather.conditiontext = conditionnode.getattributes()        .getnameditem("text")        .getnodevalue()        .tostring();      myweather.conditiondate = conditionnode.getattributes()        .getnameditem("date")        .getnodevalue()        .tostring();       //added elements of <yweather:forecast.../>      nodelist forecastlist = srcdoc.getelementsbytagname("yweather:forecast");       myweather.forecast = "";      if(forecastlist.getlength() > 0){       myweather.numberofforecast = string.valueof(forecastlist.getlength());       for(int = 0; < forecastlist.getlength(); i++){        node forecastnode = forecastlist.item(i);        myweather.forecast +=          forecastnode           .getattributes()           .getnameditem("date")           .getnodevalue()           .tostring() + " " +          forecastnode           .getattributes()           .getnameditem("text")           .getnodevalue()           .tostring() +          " high: " + forecastnode              .getattributes()              .getnameditem("high")              .getnodevalue()              .tostring() +          " low: " + forecastnode              .getattributes()              .getnameditem("low")              .getnodevalue()              .tostring() + "\n";       }      }else{       myweather.numberofforecast = "no forecast";      }       return myweather;      }      private document convertstringtodocument(string src){       document dest = null;      documentbuilderfactory dbfactory =        documentbuilderfactory.newinstance();      documentbuilder parser;       try {       parser = dbfactory.newdocumentbuilder();       dest = parser.parse(new bytearrayinputstream(src.getbytes()));       } catch (parserconfigurationexception e1) {       e1.printstacktrace();       toast.maketext(mainactivity.this,         e1.tostring(), toast.length_long).show();       } catch (saxexception e) {       e.printstacktrace();       toast.maketext(mainactivity.this,         e.tostring(), toast.length_long).show();       } catch (ioexception e) {       e.printstacktrace();       toast.maketext(mainactivity.this,         e.tostring(), toast.length_long).show();       }       return dest;      }      private string queryyahooweather(){       string qresult = "";      string querystring = "http://weather.yahooapis.com/forecastrss?w=1062617&u=c";       httpclient httpclient = new defaulthttpclient();      httpget httpget = new httpget(querystring);       try {       httpentity httpentity = httpclient.execute(httpget).getentity();        if (httpentity != null){        inputstream inputstream = httpentity.getcontent();        reader in = new inputstreamreader(inputstream);        bufferedreader bufferedreader = new bufferedreader(in);        stringbuilder stringbuilder = new stringbuilder();         string stringreadline = null;         while ((stringreadline = bufferedreader.readline()) != null) {         stringbuilder.append(stringreadline + "\n");         }         qresult = stringbuilder.tostring();        }       } catch (clientprotocolexception e) {       e.printstacktrace();       toast.maketext(mainactivity.this,         e.tostring(), toast.length_long).show();       } catch (ioexception e) {       e.printstacktrace();       toast.maketext(mainactivity.this,         e.tostring(), toast.length_long).show();       }       return qresult;      }  } 

still not working?

i made project myself, , if work. :)

the r.id , drawables not same, need change make work

 textview weather;  imageview forecast;  private static handler mhandler = new handler();  class myweather{     string conditiontext;   string conditiondate;    string numberofforecast;   string forecast;    public string forecasttostring(){     return "\n- "             + "weather: " +"you wanted have here, dont understand what. " + "\n"      + "condition: " + conditiontext + "\n"     + conditiondate +"\n"      + "\n"     + "number of forecast: " + numberofforecast + "\n"     + forecast;    }   }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         weather = (textview)findviewbyid(r.id.textview1);         forecast = (imageview)findviewbyid(r.id.imageview1);          thread mythread = new thread(new runnable(){     @override    public void run() {     string weatherstring = queryyahooweather();           document weatherdoc = convertstringtodocument(weatherstring);            final myweather weatherresult = parseweather(weatherdoc);           runonuithread(new runnable(){       @override      public void run() {       weather.settext(weatherresult.forecasttostring());      }});     }});         mythread.start();     }      private myweather parseweather(document srcdoc){       myweather myweather = new myweather();          //<yweather:condition.../>      node conditionnode = srcdoc.getelementsbytagname("yweather:condition").item(0);        string weathercode = conditionnode.getattributes()            .getnameditem("code")            .getnodevalue()            .tostring();       if(weathercode.equals("28")){           mhandler.post(new runnable() {             @override             public void run() {                 // gets executed on ui thread can safely modify                 // views                   forecast.setimageresource(r.drawable.ic_launcher);             }         });       }      ... 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -