删除后尝试刷新Universal Image Loader中的Gridview

时间:2013-10-07 14:31:47

标签: android gridview universal-image-loader

我在带有Universal Image Loader的GridView中显示图像。图像来自数据库。我添加了一个上下文菜单,根据需要从数据库中删除图片。一切都很顺利。删除图片后,我需要它来刷新视图,但它仍然显示已删除的图片。 (我使用UIL的默认设置并且没有启用缓存)我试图调用destroy然后初始化到imageloader,然后调用异步任务再次查询图像,但它给出了一个错误,说找不到文件(删除的文件) )。这是我的(冗长的)代码。谁能看到我错过的东西?

GridView gridview;
AdapterContextMenuInfo info;
ImageLoaderConfiguration config;
ImageView imageView;  
ArrayList<String> pictures = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ac_image_grid);

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

class getalbum extends AsyncTask<String, String, String> {

    //pre execute here  

    @Override
    protected String doInBackground(String... args) {
        int success;

        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", restoredemail));
            params.add(new BasicNameValuePair("album_name", album_name));

            json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
            success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                mPictures = json.getJSONArray(TAG_POSTS);

                // looping through all posts according to the json object
                // returned

                for (int i = 0; i < mPictures.length(); i++) {
                    JSONObject c = mPictures.getJSONObject(i);

                    String dir = c.getString("dir");
                    String album = c.getString("album");
                    String pic = c.getString("photo");

                    String picture = thecompletedpicture;
                    pictures.add(picture);
                }
                return json.getString(TAG_MESSAGE);
            } else {
                Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        setpictures();
    }
}

private void setpictures() {
    // TODO Auto-generated method stub
    gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this, pictures));
    registerForContextMenu(gridview);
    gridview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            startImagePagerActivity(position);
        }
    });
}



// and then finally the context menu makes 
// an async thread to delete the picture and 
// remove it from the database. On the 
// post execute I put the call to load the pictures again

// This is where the problem is. I need it to just display
// the remaining pictures. Maybe trying to remove the deleted 
// picture from the array and then reloading it?  I really need
// help with the code.

protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();

        imgLoader.destroy();
        imgLoader.init(config);

        new getalbum().execute();

    }

1 个答案:

答案 0 :(得分:0)

尝试使用它清空图像缓存:

Collection<String> c = ImageLoader.getInstance().getMemoryCache().keys();
for(String key : c) {
    try {
        MemoryCacheUtil.removeFromCache(key, ImageLoader.getInstance().getMemoryCache());
    } catch(Exception ex) {ex.printStackTrace();}
}