2013-07-18 34 views
0

我想從drawable顯示圖像。 但是,我認爲如果我有其他問題 ,因爲顯示圖像的聲明是正確的。 和ya, 我想根據天氣文本顯示圖像。 如 <yweather:condition text="Light Rain" code="11" temp="26" date="Thu, 18 Jul 2013 12:00 pm SGT" />如何從drawable顯示圖像?

,所以當我的代碼,它會顯示爲「Logo.png」文本「小雨」

這裏是我的代碼。

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 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

'開關(conditionNode.getTextContent()){ 情況下爲 「11」:image.setImageResource(R.drawable.code11);打破; }' – Gina

+0

嗨, 發生錯誤 Android需要編譯器合規性級別5.0或6.0。取而代之的是「1.7」。請使用Android工具>修復項目屬性。 – Febbie

+0

http://stackoverflow.com/questions/7637144/android-requires-compiler-compliance-level-5-0-or-6-0-found-1-7-instead-plea – Gina

回答

0
switch(Integer.valueOf(conditionNode.getTextContent())){ 
    case 11: 
     image.setImageResource(R.drawable.code11); 
     break; 
    default: 
     image.setImageResource(R.drawable.old); 
} 
+0

logcat的錯誤 八月7日至19日:59:35.879:E/AlarmManagerService(1707):android_server_AlarmManagerService_set到type = 1,1374195576.884000000 – Febbie

+0

我不知道這件事,當你使用if else的時候會發生嗎? – Gina

+0

嗨,謝謝。 我可以讓我的代碼已經工作。 – Febbie