简体   繁体   中英

Can I use setImageResource in onPostexecute?

Im new/learning Java and Android, and attempting to make a simple weather app which tells me the weather in my current location. I've got the basics figured out and I'm using OpenWeatherMap APIs - all good here.

What I'm trying to do is the following: - When API returns to me that the weather is, for example, 'broken clouds', or 'heavy rain', I want to set an ImageView to a picture of, say broken clouds, or heavy rain.

I just cant seem to get the ImageView to take the images (which are in my Drawable folder) - I have tried using setImageResource, tried setImageDrawable, among others. Any pointers? What am I missing?

Here is my code (the important bits):

public class DownloadHtml extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        String result = "";
        URL url;
        HttpURLConnection urlConnection;

        try {

            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();

            while (data != -1) {

                char current = (char) data;
                result += current;
                data = reader.read();
            }

            return result;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        try {
            JSONObject jsonObject = new JSONObject(result);
            String weatherInfo = jsonObject.getString("weather");
            String tempInfo = jsonObject.getJSONObject("main").getString("temp");
            String windInfo = jsonObject.getJSONObject("wind").getString("deg");

            JSONArray weatherArr = new JSONArray(weatherInfo);
            for (int i = 0; i < weatherArr.length(); i++) {

                TextView weatherTextView = findViewById(R.id.weatherTextView);
                weatherTextView.setText("");
                JSONObject jsonPart = weatherArr.getJSONObject(i);
                String description = jsonPart.getString("description");
                weatherTextView.setText(description);
                iv = findViewById(R.id.imageView2);


                if (description == "clear sky") iv.setImageResource(R.drawable.clear_skies);

                else if (description == "few clouds")
                    iv.setImageResource(R.drawable.few_clouds);

                else if (description == "scattered clouds")
                    iv.setImageResource(R.drawable.scattered_clouds);

                else if (description == "broken clouds")
                    iv.setImageResource(R.drawable.broken_clouds);

                else if (description == "shower rain")
                    iv.setImageResource(R.drawable.shower_rain);

                else if (description == "rain") iv.setImageResource(R.drawable.heavy_rain);

                else if (description == "thunderstorm")
                    iv.setImageResource(R.drawable.thunderstorm);

                else if (description == "snow") iv.setImageResource(R.drawable.snow);

                else if (description == "mist") iv.setImageResource(R.drawable.mist);

                Log.i("description", jsonPart.getString("description"));

                if (description != null) {
                    locationManager.removeUpdates(locationListener);
                }
            }

            for (int f = 0; f < tempInfo.length(); f++) {
                TextView weatherTextView3 = findViewById(R.id.weatherTextView3);
                weatherTextView3.setText("");
                String tempString = tempInfo + " degrees celcius.\n";
                weatherTextView3.setText(tempString);
                Log.i("temp: ", tempInfo);


                if (tempString != null) {
                    locationManager.removeUpdates(locationListener);
                    f = 500;
                }
            }

            for (int j = 0; j < windInfo.length(); j++) {
                TextView weatherTextView2 = findViewById(R.id.weatherTextView2);
                weatherTextView2.setText("");
                // assign text to wind direction
                double  windDegrees = Double.valueOf(windInfo);
                if ((windDegrees > 0) && (windDegrees < 90)) {
                    String windString = "A North East Wind.\n";
                    weatherTextView2.setText(windString);
                    Log.i("Wind: ", windInfo);

                }
                if ((windDegrees >= 90) && (windDegrees < 180)) {
                    String windString = "A South East Wind.\n";
                    weatherTextView2.append(windString);
                    Log.i("Wind: ", windInfo);

                }
                if ((windDegrees >= 180) && (windDegrees < 270)) {
                    String windString = "A South West Wind.\n";
                    weatherTextView2.append(windString);
                    Log.i("Wind: ", windInfo);

                }
                if ((windDegrees >= 270) && (windDegrees < 360)) {
                    String windString = "A North West Wind.\n";
                    weatherTextView2.append(windString);
                    Log.i("Wind: ", windInfo);

                }
                if (windInfo != null) {
                    locationManager.removeUpdates(locationListener);
                    j = 500;
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

You just accidentally used == to compare strings :)

Use description.equals("clear sky") instead of description == "clear sky"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM