【问题标题】:How to display image based on weather from drawable如何根据drawable中的天气显示图像
【发布时间】:2013-07-18 01:11:43
【问题描述】:

我想根据天气显示图像。 我得到的图像来自drawable。

例如: 如果code = 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; 
    }

}

【问题讨论】:

  • ImageImageView 需要根据天气更新吗?哪个变量保存天气所依赖的值?
  • 你好,是的,需要根据天气更新。节点条件Node = srcDoc.getElementsByTagName("yweather:condition").item(0); myWeather.conditiontext1=""; myWeather.conditiontext1 = conditionNode.getAttributes().getNamedItem("code").getNodeValue().toString(); if(myWeather.conditiontext1 == "4"){ //Drawable myDrawable = getResources().getDrawable(R.drawable.logo); // Image.setImageDrawable(myDrawable) ; Image.setImageResource(R.drawable.logo); }

标签: android eclipse weather


【解决方案1】:

还是不行?

我自己制作了这个项目,如果你这样做,它会起作用。 :)

R.id 和 drawables 不一样,你需要改变它们才能工作

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

     }
     ...

【讨论】:

  • 你的 logcat 说什么?
  • 也许 toString() 函数把事情搞砸了,因为字符串有它自己的 toString() 函数。可能想将函数的名称更改为 forecastToString() 之类的。
  • 嗨,我刚刚更改了代码。以上是我最新的代码。 GUI 没有显示图像,但是它写了一些“android.widget.ImageView@4055c7f8”
  • 为什么不使用:Image.setImageDrawable(R.drawable.logo);可能是问题所在。
  • setImageDrawable这部分有错误,已经下划线了
【解决方案2】:

我在你的代码中发现了一些问题,有两个图像视图

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

问题 - : 图片名称丢失

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

//你为什么使用动态图像视图

你应该使用处理线程来显示图像。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 2017-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多