gpt4 book ai didi

android - 在 asynctask 中使用 asynctask

转载 作者:太空狗 更新时间:2023-10-29 16:24:03 47 4
gpt4 key购买 nike

我想使用 AsyncTask 将图像加载到 ListView。

private class LoadImageTask extends AsyncTask<HashMap<String,Bitmap>,Void,Bitmap>{

@SuppressWarnings("unchecked")
@Override
protected void onPostExecute(Bitmap result) {

if(model.getIconCache().get(cellIcon)!=null){
icon.setImageBitmap(model.getIconCache().get(cellIcon));
}else{
new LoadImageTask().execute(model.getIconCache());

}


}

@Override
protected Bitmap doInBackground(HashMap<String, Bitmap>... params) {
//return model.getIconCache().get(cellIcon);
return null;

}

}

好的,我知道这不是情感代码。但是它运行良好,但需要大量内存分配。在阅读有关 AsyncTask 的文档时,它说 Asynctask 只能从 UI 线程调用,它怎么能让它在内部使用?当然,我想让我的代码在单个 AsyncTask 中运行。代码中的“模型”是一个对象,在运行时通过另一个线程更新。所以我需要找到一种方法来使用单个 Asynctask 来定期控制对象的状态。我怎么做?谢谢

最佳答案

仅在 backGround 中执行在 backGround 线程上运行,而 postExecute 和 preExecute 在 UI 线程本身上运行。出于同样的原因,您可以在其中显示和关闭对话框。

如果您想将单个 Asynctask 用于多种用途,您可以通过在 .execute() 方法中传递不同的常量来玩弄..

我的意思是这样的。

Integer Constant1 = 1;
int Constant2 = 2;

在打电话的时候,

new Background.execute(Constan1 or 2)

在 AsyncTask 中

doInBackground(Object.. arg0)
{
if(arg0.equals())
{

}
else if(arg0.equals())
{
}
}

关于android - 在 asynctask 中使用 asynctask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6731919/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com