我試圖從我的適配器中使用Interface
和AsyncTask
時得到java.lang.ClassCastException
。我不知道這是否是'允許的',但我不明白爲什麼只要一切都保持在上下文中就不會。類別轉換異常,從AsyncTask內部適配器
當我嘗試使用自定義Adapter
設置我的GridView
時,代碼開始於我的Fragment
。
mSceneGridAdapter = new SceneGridAdapter(getActivity(), mScenesList);
導致我SceneGridAdapter類被稱爲
public class SceneGridAdapter extends BaseAdapter implements HandleProductThumbnail {
private Context mContext;
private ArrayList<Scene> mSceneList = null;
private LayoutInflater mInflater;
private Drawable mThumbnail;
public SceneGridAdapter() {}
public SceneGridAdapter(Context context, ArrayList<Scene> sceneList) {
super();
mContext = context;
if (mSceneList != null) {
clearData();
} else {
mSceneList = sceneList;
}
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.scene_cell, null);
}
ImageView sceneView = (ImageView) convertView.findViewById(R.id.sceneImage);
Scene scene = mSceneList.get(position);
String specifier = scene.getSpecifier() + "/" + scene.getName();
LoadScenes scenes = new LoadScenes(mContext, specifier);
scenes.execute();
sceneView.setBackground(mThumbnail);
return convertView;
}
@Override
public int getCount() {
return mSceneList.size();
}
@Override
public Object getItem(int position) {
return mSceneList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public void clearData() {
mSceneList.clear();
}
@Override
public void handleProductThumbnail(Drawable thumb) {
mThumbnail = thumb;
}
}
它實現了我的Interface
HandleProductThumbnails
public interface HandleProductThumbnail {
void handleProductThumbnail(Drawable thumb);
}
,然後叫我叫LoadScenes AsyncTask
裝載機類,它加載縮略圖和回報他們到適配器。
public class LoadScenes extends AsyncTask<Void, Void, Drawable> {
private static final String LOADING = "Loading...";
private static final String PNG = "_ORIGINAL.PNG";
private Context mContext;
private HandleProductThumbnail mHandleThumbnail = null;
private ProgressDialog mProgressDialog;
private String mSpecifier;
public LoadScenes() {}
public LoadScenes (Context context, String specifier) {
mContext = context;
mSpecifier = specifier;
}
@Override
public void onPreExecute() {
mProgressDialog = new ProgressDialog(mContext);
mProgressDialog.setMessage(LOADING);
mProgressDialog.show();
}
public Drawable doInBackground(Void... v) {
Drawable productThumbnail = getDrawable(mSpecifier);
return productThumbnail;
}
public void onPostExecute(Drawable productThumbnail) {
mProgressDialog.dismiss();
mHandleThumbnail = (HandleProductThumbnail) mContext;
mHandleThumbnail.handleProductThumbnail(productThumbnail);
}
public Drawable getDrawable(String specifier) {
Bitmap bmp = null;
Drawable productThumbnail = null;
specifier = specifier.replace("\\", "/") + PNG;
try {
bmp = BitmapFactory.decodeStream(mContext.getAssets().open(specifier));
if (bmp != null) {
int width = 300;
int height = 240;
bmp = Bitmap.createScaledBitmap(bmp, width, height, true);
productThumbnail = new BitmapDrawable(mContext.getResources(), bmp);
}
} catch (IOException e) {
e.printStackTrace();
}
return productThumbnail;
}
}
,最後我的日誌
09-30 10:34:35.725: E/AndroidRuntime(22442): java.lang.ClassCastException: com.example.xxx.visualizer.VisualizerActivity cannot be cast to com.example.xxx.interfaces.HandleProductThumbnail
09-30 10:34:35.725: E/AndroidRuntime(22442): at com.example.xxx.visualizer.LoadScenes.onPostExecute(LoadScenes.java:46)
的Fragment
連接到名爲VisualizerActivity的Activity
,並正在傳遞的所有方面來自於Activity
。雖然這個問題有點層次,但代碼很模板化。我想知道我是否正在做一些不允許的事情?從我的理解Context
這應該工作。請讓我知道我做錯了什麼,爲什麼,如果它可以修復。謝謝
這是正確的,我的活動沒有實現我的接口。我的適配器是。我的適配器在設置我的GridView之前執行此操作。如果我將Drawable返回到我的活動中,那麼我將如何將它傳遞迴適配器?你看到問題了嗎? – Jawascript 2014-09-30 15:10:54
您不會將drawable返回到活動,而是返回適配器。特別是你將結果返回給類接口 – Blackbelt 2014-09-30 15:12:04
這對我有用。謝謝! – Jawascript 2014-09-30 15:33:05