如何根据从绘制天气显示图像 [英] How to display image based on weather from drawable

查看:142
本文介绍了如何根据从绘制天气显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示根据天气的图像。
我是从得到的图片是从绘制。

例如:
如果code = 30,将显示雨天图标

我是从

检索它

 < yweather:状态文字=小雨用迅雷code =4TEMP =25日期=星期四,2013年7月18日7:58 SGT/>

下面是我的code

MainActivity.java

 公共类MainActivity延伸活动{ TextView的天气;
 ImageView的形象; 类MyWeather {
  串conditiontext;
  串conditiondate;  串numberOfForecast;
  串预测;  公共字符串的toString(){   回归\\正
           +天气+图像+\\ n    +条件:+ conditiontext +\\ n
    + conditiondate +\\ n    +\\ n
    +号预测:+ numberOfForecast +\\ n
    +预测;  }
 }    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        天气=(的TextView)findViewById(R.id.weather);
        图像=(ImageView的)findViewById(R.id.image);        线程MyThread的=新主题(新的Runnable(){   @覆盖
   公共无效的run(){
    串weatherString = QueryYahooWeather();
          文档weatherDoc = convertStringToDocument(weatherString);          最后MyWeather weatherResult = parseWeather(weatherDoc);
          runOnUiThread(新的Runnable(){     @覆盖
     公共无效的run(){
      weather.setText(weatherResult.toString());
     }});   }});
        myThread.start();
    }    私人MyWeather parseWeather(文档srcDoc){     MyWeather myWeather =新MyWeather();        //<yweather:condition.../>
     节点conditionNode = srcDoc.getElementsByTagName(yweather:条件)。项目(0);
     如果(conditionNode.getTextContent()。等于(11)){
         image.setImageResource(R.drawable.logo);
     }
     其他{     image.setImageResource(R.drawable.old);
     }
     myWeather.conditiontext = conditionNode.getAttributes()
       .getNamedItem(文本)
       .getNodeValue()
       的ToString();
     myWeather.conditiondate = conditionNode.getAttributes()
       .getNamedItem(DATE)
       .getNodeValue()
       的ToString();     //相加得到的&LT元素; yweather:预测... /&GT;
     节点列表forecastList = srcDoc.getElementsByTagName(yweather:预期);     myWeather.forecast =;
     如果(forecastList.getLength()大于0){
      myWeather.numberOfForecast =将String.valueOf(forecastList.getLength());
      的for(int i = 0; I&LT; forecastList.getLength();我++){
       节点forecastNode = forecastList.item(ⅰ);
       myWeather.forecast + =
         forecastNode
          .getAttributes()
          .getNamedItem(DATE)
          .getNodeValue()
          的ToString()++
         forecastNode
          .getAttributes()
          .getNamedItem(文本)
          .getNodeValue()
          的ToString()+
         高:+ forecastNode
             .getAttributes()
             .getNamedItem(高)
             .getNodeValue()
             的ToString()+
         低+ forecastNode
             .getAttributes()
             .getNamedItem(低)
             .getNodeValue()
             的ToString()+\\ n;
      }
     }其他{
      myWeather.numberOfForecast =没有预测;
     }     返回myWeather;
    }    私人文件convertStringToDocument(的字符串src){     文档DEST = NULL;
     的DocumentBuilderFactory dbFactory =
       DocumentBuilderFactory.newInstance();
     的DocumentBuilder分析器;     尝试{
      解析器= dbFactory.newDocumentBuilder();
      DEST = parser.parse(新ByteArrayInputStream进行(src.getBytes()));
     }赶上(E1的ParserConfigurationException){
      e1.printStackTrace();
      Toast.makeText(MainActivity.this,
        e1.toString(),Toast.LENGTH_LONG).show();
     }赶上(SAXException的E){
      e.printStackTrace();
      Toast.makeText(MainActivity.this,
        e.toString(),Toast.LENGTH_LONG).show();
     }赶上(IOException异常五){
      e.printStackTrace();
      Toast.makeText(MainActivity.this,
        e.toString(),Toast.LENGTH_LONG).show();
     }     返回DEST;
    }    私人字符串QueryYahooWeather(){     串QRESULT =;
     字符串的queryString =htt​​p://weather.yahooapis.com/forecastrss?w=1062617&u=c;     HttpClient的HttpClient的=新DefaultHttpClient();
     HTTPGET HTTPGET =新HTTPGET(的queryString);     尝试{
      HttpEntity httpEntity = httpClient.execute(HTTPGET).getEntity();      如果(httpEntity!= NULL){
       为InputStream的InputStream = httpEntity.getContent();
       读者=新的InputStreamReader(InputStream的);
       的BufferedReader的BufferedReader =新的BufferedReader(中);
       StringBuilder的StringBuilder的=新的StringBuilder();       字符串stringReadLine = NULL;       而((stringReadLine = bufferedreader.readLine())!= NULL){
        stringBuilder.append(stringReadLine +\\ n);
       }       QRESULT = stringBuilder.toString();
      }
     }赶上(ClientProtocolException E){
      e.printStackTrace();
      Toast.makeText(MainActivity.this,
        e.toString(),Toast.LENGTH_LONG).show();
     }赶上(IOException异常五){
      e.printStackTrace();
      Toast.makeText(MainActivity.this,
        e.toString(),Toast.LENGTH_LONG).show();
     }     返回QRESULT;
    }}


解决方案

还是不工作?

我做出了自己的项目,如果你不喜欢这样它会工作。 :)

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

  TextView的天气;
 ImageView的预测;
 私有静态处理程序mHandler =新的处理程序();
 类MyWeather {
  串conditiontext;
  串conditiondate;  串numberOfForecast;
  串预测;  公共字符串forecastToString(){   回归\\正
           +天气+你想在这里有什么东西,我只是不明白。+\\ n    +条件:+ conditiontext +\\ n
    + conditiondate +\\ n    +\\ n
    +号预测:+ numberOfForecast +\\ n
    +预测;  }
 }    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        天气=(的TextView)findViewById(R.id.textView1);
        预测=(ImageView的)findViewById(R.id.imageView1);        线程MyThread的=新主题(新的Runnable(){   @覆盖
   公共无效的run(){
    串weatherString = QueryYahooWeather();
          文档weatherDoc = convertStringToDocument(weatherString);          最后MyWeather weatherResult = parseWeather(weatherDoc);
          runOnUiThread(新的Runnable(){     @覆盖
     公共无效的run(){
      weather.setText(weatherResult.forecastToString());
     }});   }});
        myThread.start();
    }    私人MyWeather parseWeather(文档srcDoc){     MyWeather myWeather =新MyWeather();        //<yweather:condition.../>
     节点conditionNode = srcDoc.getElementsByTagName(yweather:条件)。项目(0);
     串天气code = conditionNode.getAttributes()
           .getNamedItem(code)
           .getNodeValue()
           的ToString();     如果(天气code.equals(28)){         mHandler.post(新的Runnable(){
            @覆盖
            公共无效的run(){
                //这个被UI线程上执行,因此可以安全地修改
                //浏览                 forecast.setImageResource(R.drawable.ic_launcher);
            }
        });     }
     ...

i would like to display an image based on the weather. The image that I'm getting from is from drawable.

For example : if the code = 30, it will display rainy day icon

I'm retrieving it from

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

Here is my 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 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; 
    }

}

解决方案

Still not working?

I made the project myself, and if you do it like this it will work. :)

The R.id and drawables are not the same, you need to change those to make it 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 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);
            }
        });

     }
     ...

这篇关于如何根据从绘制天气显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆