Question

Hey I'm using the UniversalImageLoader in my android app currently what it does is load images from a weather api but whenever I get new data the Image loader does not load the new images.

This is how I would like it to work when the api returns data the ImageLoader loads the image from the url and when I get new data the new images are loaded in the same place where the old images were. How can I make sure that the Image Loader always loads the new urls in my app.

Here is my code :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.weather, container, false);
    int i = getArguments().getInt(ARG_MENU_NUMBER);
    String titleString = getResources().getStringArray(R.array.drawer_menu)[i];
    getActivity().setTitle(titleString);

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getActivity()).build();
    ImageLoader.getInstance().init(config);

    options = new DisplayImageOptions.Builder()
            .displayer(new RoundedBitmapDisplayer(2)).build();
    return view;
}
public void processJson(String json) {
    w.setText("Wind Speed");
    ws.setText("Wind Direction");
    hu.setText("Humidity");
    pr.setText("Pressure");
    String weatherDescription = null;
    String weatherUrl = null;
    Calendar calender = Calendar.getInstance();



    try {
        JSONObject jObj = new JSONObject(json);
            JSONObject jobject = jObj.getJSONObject("data");
            JSONArray jarrayConditions = jobject.getJSONArray("current_condition");
            for(int i = 0;i < jarrayConditions.length();i++){
                JSONObject jobject2 = jarrayConditions.getJSONObject(i);
                String tempc = jobject2.getString("temp_C");
                String windSpeed = jobject2.getString("windspeedKmph");
                String windDirections = jobject2.getString("winddirDegree");
                String airHumidity = jobject2.getString("humidity");
                String airPressure = jobject2.getString("pressure");
                for(int j = 0; j < jobject2.getJSONArray("weatherDesc").length();j++){
                    weatherDescription = jobject2.getJSONArray("weatherDesc").getJSONObject(j).getString("value");

                }
                wind.setText(windSpeed+ " m/s");
                windDirection.setText(windDirections+ "\u00B0");
                humidity.setText(airHumidity+ " %");
                pressure.setText(airPressure+ " hPa");
                weather.setText(weatherDescription);
                temp.setText(new DecimalFormat("#").format(jarrayConditions.getJSONObject(i).getDouble("temp_C"))
                        + "\u2103");
            }
            JSONArray jarrayWeather = jobject.getJSONArray("weather");
            for(int i = 0;i < jarrayWeather.length();i++){
                JSONObject jobject3 = jarrayWeather.getJSONObject(i);
                String daysTempsH = jobject3.getString("tempMaxC");
                String daysTempsL = jobject3.getString("tempMinC");
                String date = jobject3.getString("date");
                for(int j = 0; j < jobject3.getJSONArray("weatherIconUrl").length();j++){
                    weatherUrl = jobject3.getJSONArray("weatherIconUrl").getJSONObject(j).getString("value");

                }
                mArrayHigh.add(daysTempsH);
                mArrayLow.add(daysTempsL);
                mArrayImage.add(weatherUrl);
                mArrayDate.add(date);
            }

            min.setText("L "+mArrayLow.get(0)+ "\u2103");
            max.setText("H "+mArrayHigh.get(0)+ "\u2103");

            tvDay1L.setText("L "+mArrayLow.get(1)+ "\u2103");
            tvDay1H.setText("H "+mArrayHigh.get(1)+ "\u2103");
            calender.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(mArrayDate.get(1)));
            tvDay1.setText(getDayOfWeek(calender.get(Calendar.DAY_OF_WEEK)));

            String imageUrl1 = mArrayImage.get(1);

            ImageLoader.getInstance()
                    .displayImage(imageUrl1, day1Icon, options);


            tvDay2L.setText("L "+mArrayLow.get(2)+ "\u2103");
            tvDay2H.setText("H "+mArrayHigh.get(2)+ "\u2103");
            calender.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(mArrayDate.get(2)));
            tvDay2.setText(getDayOfWeek(calender.get(Calendar.DAY_OF_WEEK)));
            String imageUrl2 = mArrayImage.get(2);
            ImageLoader.getInstance()
                    .displayImage(imageUrl2, day2Icon, options);
            tvDay3L.setText("L "+mArrayLow.get(3)+ "\u2103");
            tvDay3H.setText("H "+mArrayHigh.get(3)+ "\u2103");
            calender.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(mArrayDate.get(3)));
            tvDay3.setText(getDayOfWeek(calender.get(Calendar.DAY_OF_WEEK)));
            String imageUrl3 = mArrayImage.get(2);
            ImageLoader.getInstance()
                    .displayImage(imageUrl3, day3Icon, options);
            tvDay4L.setText("L "+mArrayLow.get(4));
            tvDay4H.setText("H "+mArrayHigh.get(4));
            calender.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(mArrayDate.get(4)));
            tvDay4.setText(getDayOfWeek(calender.get(Calendar.DAY_OF_WEEK)));
            String imageUrl4 = mArrayImage.get(4);
            ImageLoader.getInstance()
                    .displayImage(imageUrl4, day4Icon, options);

    }catch(Exception e){
        e.printStackTrace();
    }
}
Was it helpful?

Solution 2

Hey i figured this out by updating to the latest Universal image loader released and the simply added this few lines of code

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            getActivity()).build();
    ImageLoader.getInstance().init(config);

    options = new DisplayImageOptions.Builder()
    .showImageOnLoading(android.R.color.transparent)
    .showImageForEmptyUri(R.drawable.cloud)
    .showImageOnFail(R.drawable.cloud).cacheInMemory(true)
    .cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();

OTHER TIPS

please change your DisplayOptions like this and try it

options = new DisplayImageOptions.Builder()
            .showImageOnLoading(android.R.color.transparent)
            .showImageForEmptyUri(R.drawable.ic_empty)
            .showImageOnFail(R.drawable.ic_empty).cacheInMemory(true)
            .cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top