[问题] DialogFragment+BaseAdapter+AsyncTask

楼主: comteken ([CR])   2014-07-24 21:41:16
public class CustomAdapter extends BaseAdapter {
private static final String TAG = "CustomAdapter";
ArrayList<Bitmap> _bitmapList;
LayoutInflater inflater;
public CustomAdapter(Context context) {
_bitmapList = new ArrayList<Bitmap>();
inflater = LayoutInflater.from(context);
}
public void addBitmap(Bitmap bitmap) {
try {
_bitmapList.add(bitmap);
Log.d(TAG, "_bitmapList.size() = " + _bitmapList.size());
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
convertView = inflater.inflate(
R.layout.custom_adapter_listview_item, null);
viewHolder.iv = (ImageView) convertView
.findViewById(R.id.custom_adapter_listview_item_imageView);
convertView.setTag(viewHolder);
} else {
viewHolder.iv = (ImageView) convertView.getTag();
}
viewHolder.iv.setImageBitmap(_bitmapList.get(position));
return convertView;
}
class ViewHolder {
ImageView iv;
}
}
public class MorePhotoDialog extends DialogFragment {
private static final String TAG = "MorePhotoDialog";
private CustomAdapter _customAdapter;
private Context _context;
private ListView _listView;
public MorePhotoDialog(Context context) {
Log.d(TAG, "new MorePhotoDialog");
_customAdapter = new CustomAdapter(context);
_context = context;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.custom_adapter, null);
_listView = (ListView) view.findViewById(R.id.custom_adapter_listView);
_listView.setAdapter(_customAdapter);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setTitle(R.string.more_photo_dialog_more);
return builder.create();
}
public void addImageLoaderTask(String... urls) {
ImageLoaderTask imageLoaderTask = new ImageLoaderTask();
// Log.d(TAG, "imageLoaderTask.execute: " + urls[i]);
imageLoaderTask.execute(urls);
}
private class ImageLoaderTask extends AsyncTask<String, Void, Bitmap[]> {
// private class ImageLoaderTask extends AsyncTask<String, Void, Void> {
@Override
protected Bitmap[] doInBackground(String... url) {
// protected Void doInBackground(String... url) {
Bitmap[] bitmaps = new Bitmap[url.length];
for (int i = 0; i < url.length - 1; i++) {
String imgUrl = url[i];
Log.w(TAG, "url[0] = " + url[i]);
// Getting Caching directory
File cacheDirectory = _context.getCacheDir();
// Temporary file to store the downloaded image
File tmpFile = new File(cacheDirectory.getPath(),
FunctionUtility.getLastString(imgUrl));
Log.d(TAG, "imgUrl(" + tmpFile.getName() + ") = " + imgUrl);
if (!tmpFile.exists()) {
Log.w(TAG, tmpFile.getPath() + " not exists");
BitmapFuntions.downloadBitmapFromUrl(imgUrl, tmpFile);
}
// Load bitmap and put in adapter
bitmaps[i] = BitmapFuntions.decodeFile(tmpFile, 300, 200);
// Show Logs
Log.d(TAG,
"bitmap.getByteCount() = " + bitmaps[i].getByteCount());
Log.d(TAG, tmpFile.getPath());
Log.d(TAG, tmpFile.getName() + " = " + tmpFile.length() / 1024
+ " Kb");
Log.d(TAG, "imageLoaderTask.execute: " + url[i]);
}
return bitmaps;
}
@Override
protected void onPostExecute(Bitmap[] result) {
try {
Log.d(TAG, "onPostExecute");
for (int i = 0; i < result.length; i++) {
_customAdapter.addBitmap(result[i]);
}
// _customAdapter.notifyDataSetChanged();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
不知道为什么用了 _customAdapter.notifyDataSetChanged();
这一行,就crash了..
在加了这行之前都没有问题
还是有别的方式可以更新UI吗?
希望有大大可以帮小弟解答一下>"<
作者: givemepass (λ)   2014-07-24 22:09:00
Log是?讯息就是为什么了!
作者: kewang (652公共汽车)   2014-07-24 23:18:00
你的tag是存ViewHolder,可是你getTag的时候却是casting成ImageView,你不觉得哪些有问题吗?

Links booklink

Contact Us: admin [ a t ] ucptt.com