最初我从服务器获取数据列表并将其设置为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();
}
}
}
发布您的代码。 – Blackbelt
如果您正在下载图片并显示,我建议您使用UIL。如果您不喜欢使用第三方库,请以任何方式发布代码 – Raghunandan
尝试从您的asynctask的onProgressUpdate()方法中更新您的UI。 – Jarvis