我需要将两个参数(lv 和位置)传递给 Asynctask doInBackground 方法,但我不知道该怎么做...
LoadSound.execute(lv,position)
class LoadSound extends AsyncTask<**[ListView,int]**, String, String>
谢谢!
编辑:
我有一个 ListView 。如果你点击一个项目播放声音(来自互联网)。
我想用 Asynctask 显示一个 progressDialog。
在 doInBackground 方法中我有 itemOnClick:
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
因此我需要传递 lv 和 position。
最佳答案
尝试在您的 AsyncTask
中创建一个构造函数,无论何时您创建您的对象,您都可以传递参数。
像这样:
MyAsyncTask asynctask = new MyAsyncTask(10, true, myObject);
//this is how you create your object
public class MyAsyncTask extends AsyncTask<Void, Void, Void>{
int a;
boolean b;
Object c;
public MyAsyncTask(int a, boolean b, Object c){
this.a = a;
this.b = b;
this.c = c;
}
@Override
protected Void doInBackground(Void... params) {
if(b)
c = a;
return null;
}
}
然后你就可以调用asynctask.execute();
编辑:阅读您更新后的问题后,我同意 Squonk 使用服务,例如播放背景声音;你也可以在启动你的 AsynkTask 之前显示一个进度对话框(如果这个是不确定的)并在你的执行后关闭它。
关于Android:Asynctask 中的多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11335759/