【问题标题】:Trying to refresh Gridview in Universal Image Loader after delete删除后尝试在通用图像加载器中刷新 Gridview
【发布时间】:2013-10-07 14:31:46
【问题描述】:

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

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

    }

【问题讨论】:

    标签: android gridview universal-image-loader


    【解决方案1】:

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

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

    【讨论】:

    • 好吧,那仍然给我同样的错误 E/ImageLoader(31336): java.io.FileNotFoundException: mysite/uploads/20131007_085815.jpg 10-07 09:00:18.528: E/ImageLoader(31336): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) 我还注意到它在图片所在的位置留下了一个空白区域,并复制了所有其他图像哈哈。我讨厌 Gridview
    猜你喜欢
    • 1970-01-01
    • 2013-07-09
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多