我有我的方法我正在调用AsyncTask方法:
public static Drawable[] queryAppIcon() throws ParseException, IOException {
ParseQuery<ParseObject> query = ParseQuery.getQuery("AndroidStoreContent");
query.whereExists("appIcon");
List<ParseObject> ParseResult = query.find();
// initialize Drawable array
final Drawable[] appIconDrawable = new Drawable[ParseResult.size()];
for (int i = 0; i < ParseResult.size(); i++) {
ParseFile pf = (ParseFile) ParseResult.get(i).get("appIcon");
appIconDrawable[i] = DownloadImageTask.execute(pf);
}
return appIconDrawable;
}
AsyncTask:
public class DownloadImageTask extends AsyncTask<ParseFile, Void, Drawable> {
ParseFile pf = null;
@Override
protected Drawable doInBackground(ParseFile... pf) {
this.pf = pf[0];
fetchDrawable(pf);
}
protected void onPostExecute() {
// do I do anything here?
}
public Drawable fetchDrawable(ParseFile pf) {
InputStream is;
try {
is = (InputStream) new URL(pf.getUrl()).getContent();
return Drawable.createFromStream(is,null);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
我知道我应该有一个onPostExecute,但事实是Drawable的数组会进入ViewActivity的另一个类:
// Get application image from Parse
Drawable[] appIconUrl = new Drawable[0];
try {
appIconUrl = ParseContent.queryAppIcon();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
for (int i = 0; i < appText.length; i++) {
OtherRowItem item = new OtherRowItem(appIconUrl[i], appText[i]);
otherRowItems.add(item);
}
otherSize = otherRowItems.size();
otherListView = (ListView) findViewById(R.id.other_games_list);
OtherListViewAdapter other_adapter = new OtherListViewAdapter(this,
R.layout.other_list_row, otherRowItems);
otherListView.setAdapter(other_adapter);
otherListView.setOnItemClickListener(this);
ViewAdapter:
holder.imageView.setImageDrawable(otherRowItem.getAppIconUrl());
所以我无法真正在onPostExecute
中显示图像,因为它正在其他类中显示。
这个想法很简单,我有一个循环,我得到一个ParseFile然后AsyncTask获取ParseFile的内容并从中创建一个Drawable。我在没有AsyncTask的情况下测试了该方法并且它可以工作(但需要很长很长时间)。
我遇到两个错误:
1。)使用appIconDrawable[i] = DownloadImageTask.execute(pf);
,我得到:类型不匹配:无法从AsyncTask转换为Drawable 。
2。)使用fetchDrawable(pf);
,我得到 ParseContent.DownloadImageTask类型中的方法fetchDrawable(ParseFile)不适用于参数(ParseFile [])。
答案 0 :(得分:0)
On Post执行将接收你在后台创建的drawable,如果你想在UI线程上用Drawable做任何事情,这就是在哪里做的。
protected void onPostExecute(Drawable dr) { ... }
以下一行
appIconDrawable[i] = DownloadImageTask.execute(pf);
导致错误,因为execute不会像你期望的那样从doInBackground返回drawable - drawable被改为onPostExecute而不是(总是)。
你的第二个问题是一个简单的问题 - 当你只需要一个项目时,你传递的是一整套项目。如果你实际上只有一个项目,那么只是传递该元素的情况 -
fetchDrawable(pf[0]);
否则,如果您希望处理整个数组,请循环并执行多个任务。
我的建议是在创建AsyncTask时传入一个回调,你可以将其保存为成员变量,并从onPostExecute调用调用类中的方法。
public interface OnDownloadCompleteListener {
void onDownloadComplete(Drawbale dr);
}
public class MyActivity extends Activity implements OnDownloadCompleteListener {
...
private void startDownloads() {
new DownloadImageTask(this).execute();
}
public onDownloadComplete(Drawable dr) {
//when we get here the drawable is loaded, so use it however we want
}
}
public class DownloadImageTask extends AsyncTask<ParseFile, Void, Drawable> {
private OnDownloadCompleteListener mListener;
DownloadImageTask(OnDownloadCompleteListener odcl) {
mListener = odcl;
}
...
public void onPostExecute(Drawable dr) {
mListener.onDownloadComplete(dr);
}
}