android - How to display image from drawable? -
i display image drawable. however, think if else having problem because statement displaying image correct.. , ya, display image based on weather text. such <yweather:condition text="light rain" code="11" temp="26" date="thu, 18 jul 2013 12:00 pm sgt" />
so when code it, display "logo.png" text "light rain"
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); 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; } }
switch(integer.valueof(conditionnode.gettextcontent())){ case 11: image.setimageresource(r.drawable.code11); break; default: image.setimageresource(r.drawable.old); }
Comments
Post a Comment