如何使用baseadapter从gridview中删除项目而不刷新所有项目

发布于 2024-11-30 02:45:50 字数 622 浏览 1 评论 0原文

背景: 我正在使用baseadapter 用图像视图填充gridview。 Imageview的资源是使用AsyncTask下载的。我从 onPostExecute 方法收到成功失败<的回调< /强>。我在适配器的 getView 方法中使用此回调。

问题 如果 imageview 的 url 返回 404 错误,则 gridview 由于 404 错误而有一些空白。我可以删除这些项目并调用notifydatasetchanged,但这次它会从一开始就刷新所有gridview。虽然我将错误的项目保留在某处,删除它们并一次调用notifydatasetchanged,但似乎gridview在应用程序中被填充了两次。

问题 我想在 gridview 中加载图像,没有任何间隙。例如,如果有 9 个图像,第 6 个图像返回 404 错误,那么我只想在 gridview 中看到 7 个项目,

我希望我的问题很清楚。感谢您的帮助。

Background:
I'm filling gridview with imageviews using baseadapter. Imageview's resources are downloaded using AsyncTask. I get a callback from onPostExecute method as success or failure. And I use this callback in adapter's getView method.

Problem
If url for imageview returns 404 error, gridview has some empty spaces because of 404 error. I can remove those items and call notifydatasetchanged, but this time it refreshes all gridview from the start. Although i keep errored items in somewhere, remove them and call notifydatasetchanged once for all, it seems gridview is filled twice in application.

Question
I want to load images in gridview without any gaps. For instance if there are 9 images and forth and sixth images return 404 error, then i want to see only seven items in gridview

I hope my question is clear. Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2024-12-07 02:45:50

我这样解决了我的问题。当我捕获404错误的回调时,在baseadapter getview方法中,

DownloadCallback dc = new DownloadCallback(){

       public void onSuccess(String downloadedString){

       }

       public void onFailure(int callbackID){
          System.out.println("onFailure - " + callbackID);

          if( callbackID < getCount()) // in case of out of index exception
             rows.remove(callbackID); 
          //parent.removeViewAt(callbackID); we cannot remove it is UNSUPPORTED
          reloadFromPosition(callbackID, imageView, parent);
        }
     };

reloadFromPosition方法是这样的;

private void reloadFromPosition(int position, View convertView, ViewGroup parent)
{
    if( position < getCount())
    {
        System.out.println("reloadFrom - if - " + position);
        getView(position, convertView, parent);
        reloadFromPosition(position + 1, parent.getChildAt(position +1), parent);
    }

}

I solved my problem like this. When i catch a callback from 404 error, in baseadapter getview method,

DownloadCallback dc = new DownloadCallback(){

       public void onSuccess(String downloadedString){

       }

       public void onFailure(int callbackID){
          System.out.println("onFailure - " + callbackID);

          if( callbackID < getCount()) // in case of out of index exception
             rows.remove(callbackID); 
          //parent.removeViewAt(callbackID); we cannot remove it is UNSUPPORTED
          reloadFromPosition(callbackID, imageView, parent);
        }
     };

and reloadFromPosition method is like that;

private void reloadFromPosition(int position, View convertView, ViewGroup parent)
{
    if( position < getCount())
    {
        System.out.println("reloadFrom - if - " + position);
        getView(position, convertView, parent);
        reloadFromPosition(position + 1, parent.getChildAt(position +1), parent);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文