0

i would like to display image from drawable. However, I think that my if else having problem because the statement for displaying image is correct.. and ya, I would like to display the image based on the weather text. Such as <yweather:condition text="Light Rain" code="11" temp="26" date="Thu, 18 Jul 2013 12:00 pm SGT" />

so when I code it, it will display "logo.png" for the text "Light Rain"

Here are 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);

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

}
Febbie
  • 115
  • 1
  • 15
  • `switch(conditionNode.getTextContent()){ case "11": image.setImageResource(R.drawable.code11); break; }` – Gina Jul 18 '13 at 08:45
  • Hi, there is an error occurs Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools > Fix Project Properties. – Febbie Jul 18 '13 at 08:55
  • http://stackoverflow.com/questions/7637144/android-requires-compiler-compliance-level-5-0-or-6-0-found-1-7-instead-plea – Gina Jul 18 '13 at 09:08
  • I tried. then it return back to the previous error. there's underline at conditionNode.getTextContent() – Febbie Jul 18 '13 at 09:10
  • error that you never mentioned – Gina Jul 18 '13 at 09:21
  • conditionNode.getTextContent() is being underline.. – Febbie Jul 18 '13 at 09:55

1 Answers1

0
switch(Integer.valueOf(conditionNode.getTextContent())){
    case 11:
        image.setImageResource(R.drawable.code11);
        break;
    default:
        image.setImageResource(R.drawable.old);
}
Gina
  • 902
  • 8
  • 15
  • logcat error 07-19 08:59:35.879: E/AlarmManagerService(1707): android_server_AlarmManagerService_set to type=1, 1374195576.884000000 – Febbie Jul 19 '13 at 01:00
  • I don't know about that, is that happen when you use if else? – Gina Jul 19 '13 at 05:29