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嗎?
希望有大大可以幫小弟解答一下>"<