2013-07-18 27 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