通用图像加载程序仅加载任何未加载新图像的初始图像

时间:2014-05-01 09:52:28

标签: android universal-image-loader

嘿我现在在我的Android应用程序中使用UniversalImageLoader它的功能是从天气api加载图像,但每当我获得新数据时,Image加载程序都不会加载新图像。

这是我希望它在api返回数据时工作的方式ImageLoader从网址加载图像,当我获得新数据时,新图像被加载到旧图像所在的相同位置。如何确保Image Loader始终在我的应用中加载新网址。

这是我的代码:

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

2 个答案:

答案 0 :(得分:2)

请更改您的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();

答案 1 :(得分:0)

嘿,我通过更新到最新发布的Universal图像加载器并简单地添加了几行代码来解决这个问题

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