最初,我从服务器获取数据列表并将其设置为 listview。
向下滚动列表视图时,我正在从服务器收集数据并调用我的自定义适配器的 notifydatasetchanged。
在自定义适配器的 getView() 方法中,我通过 asyntask 从服务器下载图像。当它成功下载并存储在本地时。然后只是尝试在该异步任务的 onPostExecute 刷新列表视图。但它没有得到刷新。
onPostExecute 的日志正在打印,但 listview 没有刷新。
public void loadBitmap(MainActivity mainActivity, String imageKey,ImageView imageView, boolean isScrolling) { final Bitmap bitmap = 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();
}
}
}