android - Universal Image loader 只加载初始图像,不加载任何新图像

标签 android universal-image-loader

嘿,我在我的 android 应用程序中使用 UniversalImageLoader 目前它所做的是从天气 api 加载图像,但每当我获得新数据时,图像加载器不会加载新图像。

当 api 返回数据时,ImageLoader 从 url 加载图像,当我获得新数据时,新图像加载到旧图像所在的相同位置,这就是我希望它的工作方式图像是。我如何才能确保图像加载器始终在我的应用程序中加载新的 url。

这是我的代码:

@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();
    }
}

最佳答案

请像这样更改您的 DisplayOptions 并尝试

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();

关于android - Universal Image loader 只加载初始图像,不加载任何新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23405450/

相关文章:

Android Studio 3.0 : canary 6 : android. content.res.Resources$NotFoundException inside Launching Activity

android - 附加/分离与替换 fragment

java - 在 Android 中获取 "SQLiteException: no such column"

java - 使用 Universal Image Loader,内存缓存中的图像大小高于实际大小

android - 位图太大而无法上传到纹理(通用图像加载器)

Android:在一个 Activity 中向操作栏添加样式

android - 将 "inputType"设置为 "textFilter",android 是否仍保留屏幕空间以供建议?

android - UIL图像显示在GridView中imageView的中心

caching - 在 Glide 中查找并加载缓存的图像

android - Listview 上的通用图像加载器很慢