看板 AndroidDev 關於我們 聯絡資訊
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嗎? 希望有大大可以幫小弟解答一下>"< -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.36.155.148 ※ 文章網址: http://www.ptt.cc/bbs/AndroidDev/M.1406209279.A.D27.html
givemepass:Log是? 07/24 22:09
07-24 10:21:24.740: E/AndroidRuntime(20656): java.lang.ClassCastException: com.gundam.cool.component.CustomAdapter$ViewHolder cannot be cast to android.widget.ImageView ※ 編輯: comteken (114.36.155.148), 07/24/2014 22:25:18
givemepass:訊息就是為什麼了! 07/24 22:26
ViewHolder 不能這樣用嗎 直接用ImageView 是可以過的0.0 ※ 編輯: comteken (114.36.155.148), 07/24/2014 22:32:31
kewang:你的tag是存ViewHolder,可是你getTag的時候卻是casting成 07/24 23:18
kewang:ImageView,你不覺得哪些有問題嗎? 07/24 23:18
恩,腦袋突然卡住了,po上來之後就通惹0.0 ※ 編輯: comteken (114.36.155.148), 07/25/2014 03:06:55