1

最初我从服务器获取数据列表并将其设置为listview。notifyDataSetChanged不刷新列表视图

当向下滚动列表视图时,我从服务器获取数据的集合并调用我的自定义适配器的notifydatasetchanged。

在自定义适配器的getView()方法中,我通过asyntask从服务器下载图像。下载成功并在本地存储。然后试图刷新ascombask的onPostExecute列表视图。但它没有得到更新。

onPostExecute的日志正在打印,但listview没有得到刷新。

公共无效loadBitmap(MainActivity mainActivity,字符串imageKey,ImageView的ImageView的,布尔isScrolling) { 最终位图的位图= getBitmapFromCache(imageKey);

if (bitmap != null) { 
     imageView.setImageBitmap(bitmap); 
    } else { 
     imageView.setImageResource(R.drawable.ic_launcher); 
     if (!isScrolling && !mCurrentTasks.contains(imageKey) 
       && mainActivity.internetIsAvailable()) { 
      BitmapLoaderTask task = new BitmapLoaderTask(imageKey, 
        mainActivity.getAdapter()); 
      task.execute(); 
     } 
    } 
} 

private class BitmapLoaderTask extends AsyncTask<Void, Void, Bitmap> { 
    private ListAdapter mListAdapter; 
    private String mImageKey; 

    public BitmapLoaderTask(String imageKey, ListAdapter adapter) { 
     mListAdapter = adapter; 
     mImageKey = imageKey; 
    } 

    @Override 
    protected void onPreExecute() { 
     mCurrentTasks.add(mImageKey); 
    } 

    @Override 
    protected Bitmap doInBackground(Void... params) { 
     Bitmap b = null; 
     try { 
      URL url = new URL(mImageKey); 
      HttpURLConnection connection = (HttpURLConnection) url 
        .openConnection(); 
      connection.connect(); 
      b = BitmapFactory.decodeStream(connection.getInputStream()); 

      if (b != null) { 
       int width = b.getWidth(); 
       int height = b.getHeight(); 

       if (width >= mMaxWidth || height >= mMaxHeight) { 
        while (true) { 
         if (width <= mMaxWidth || height <= mMaxHeight) { 
          break; 
         } 
         width /= 2; 
         height /= 2; 
        } 
        b = Bitmap.createScaledBitmap(b, width, height, false); 
       } 

       connection.disconnect(); 
       addBitmapToCache(mImageKey, b); 
       return b; 
      } 
      return null; 
     } catch (IOException e) { 
      if (e != null) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
    } 

    @Override 
    protected void onPostExecute(Bitmap param) { 
     mCurrentTasks.remove(mImageKey); 
     if (param != null) { 
      mListAdapter.notifyDataSetChanged(); 
     } 
    } 
} 
+2

发布您的代码。 – Blackbelt

+0

如果您正在下载图片并显示,我建议您使用UIL。如果您不喜欢使用第三方库,请以任何方式发布代码 – Raghunandan

+0

尝试从您的asynctask的onProgressUpdate()方法中更新您的UI。 – Jarvis

回答

0

你的代码看起来正确的..但我觉得可能是什么问题是mainActivity.getAdapter()我想你应该申报适配器全球喜欢。

专用AdapterYourCustomAdapter适配器;

,然后在的onCreate()

adapter = new AdapterYourCustomAdapter(context,arraylist(whatever constructor is)); 

,然后通调用它喜欢:

if (bitmap != null) { 
     imageView.setImageBitmap(bitmap); 
    } else { 
     imageView.setImageResource(R.drawable.ic_launcher); 
     if (!isScrolling && !mCurrentTasks.contains(imageKey) 
       && mainActivity.internetIsAvailable()) { 
      new BitmapLoaderTask(imageKey, adapter).execute(); 
     } 
    } 
+0

我已经通过在全球范围声明但没有改进尝试过它。 – dev1993

+0

您需要更改适配器的数据源以使其通知..我的意思是在这里if(param!= null){ mListAdapter.notifyDataSetChanged(); }你没有添加或删除你的适配器的任何想法? – NaserShaikh

+0

是的。我没有添加或从我的适配器中删除。 – dev1993

0

尝试使ImageView对于每个位图入BitmapLoaderTask在施工并紧紧抓住该ImageView作为里面是一个成员变量。当您完成加载位图时,使用onPostExecute方法将该位图分配为ImageView可绘制。

private class BitmapLoaderTask extends AsyncTask<Void, Void, Bitmap> { 

    private String mImageKey; 
    private ImageView mImageView; 

    public BitmapLoaderTask(ImageView imageView, String imageKey) { 
    mImageView = imageView; 
    mImageKey = imageKey; 
    } 

    protected void onPreExecute() { 

    /* Pre execute stuff */ 

    } 

    protected Bitmap doInBackground(Void... params) { 

    /* Your bitmap processing */ 

    return bitmap; 
    } 

    protected void onPostExecute(Bitmap param) { 
    if(param != null) { 
     mImageView.setImageBitmap(param); 
    } 
    } 
} 

这是AsyncTask构建的一个完美例子。 doInBackground()方法在后台线程上运行,因此不会干扰UI处理,但android权限指示不允许此类后台线程触摸UI元素。这就是onProgressUpdate()onPostExecute()的作用,只要doInBackground达到了更新值的里程碑,它们就会在主UI线程上快速执行UI更新。在这种情况下,您正在使用它们在相应的位图准备就绪时通知ImageView对象。