2013-07-18 29 views
0

我想显示基于天气的图像。 我得到的图像是可绘制的。如何根据可绘制的天气显示图像

例如: 如果代码= 30,这将显示雨天图标

我从

<yweather:condition text="Light Rain with Thunder" code="4" temp="25" date="Thu, 18 Jul 2013 7:58 am SGT" /> 

这里获取它是我的代码

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 to get elements of <yweather:forecast.../> 
    NodeList forecastList = srcDoc.getElementsByTagName("yweather:forecast"); 

    myWeather.forecast = ""; 
    if(forecastList.getLength() > 0){ 
     myWeather.numberOfForecast = String.valueOf(forecastList.getLength()); 
     for(int i = 0; i < 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; 
    } 

} 
+0

是'Image'了'ImageView'需要被更新的每天气?什么变量持有天气依赖的价值? – Vikram

+0

嗨,是的,它需要根据天气进行更新。 Node conditionNode = srcDoc.getElementsByTagName(“yweather:condition”)。item(0); myWeather.conditiontext1 =“”; myWeather.conditiontext1 = conditionNode.getAttributes()。getNamedItem(“code”)。getNodeValue()。toString();如果(myWeather.conditiontext1 ==“4”){ \t // Drawable myDrawable = getResources()。getDrawable(R.drawable.logo); \t // Image.setImageDrawable(myDrawable); \t Image.setImageResource(R.drawable.logo); \t} – Febbie

回答

0

仍然无法正常工作?

我自己做了这个项目,如果你这样做,它就可以工作。 :)

的R.id和绘图是不一样的,你需要改变的,使其工作

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 to have something here, I just 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() { 
       // This gets executed on the UI thread so it can safely modify 
       // Views 

       forecast.setImageResource(R.drawable.ic_launcher); 
      } 
     }); 

    } 
    ... 
+0

嗨,我已经改变了代码 ,但有一个力关闭错误 – Febbie

+0

你是什么logcat说? – LordMarty

+0

也许toString()函数搞砸了,因为字符串有自己的toString()函数。 可能想要将函数的名称更改为forecastToString()或其他。 – LordMarty

0

我在代码中发现了一些问题,有两个图像争夺w^

Image = (ImageView)findViewById(R.id.image); 

问题 - :图像丢失

ImageView foreImage = new ImageView(this); 
foreImage.setImageResource(R.drawable.logo); 

//你为什么要使用动态imageview的

您应该使用处理器线程用于显示图像的名称。

+0

什么是处理程序线程? 你有样品吗? – Febbie

+0

Google bro。谷歌。 :) http://stackoverflow.com/questions/1921514/how-to-run-a-runnable-thread-in-android – LordMarty