我试图通过创建一个接口从Asynctask返回值,但在执行Asynctask类时,它给我方法调用预期错误。
我在行longoperation(dir1, asyncTask1).execute();
我该如何解决?
public interface IAsyncTask {
ArrayList<String> IAmFinished(ArrayList<File> arrayList)
}
public class MainActivity2 extends AppCompatActivity implements IAsyncTask {
public static ArrayList<File> fileList = new ArrayList<>();
public static ArrayList<String> name = new ArrayList<>();
Longoperation longoperation = new Longoperation(dir1,this);
IAsyncTask asyncTask1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
dir1 = new File(Environment.getExternalStorageDirectory().getPath());
longoperation(dir1, asyncTask1).execute();
JazzyListView lv = (JazzyListView) findViewById(R.id.list);
IAmFinished(fileList);
lv.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, name));
}
@Override
public ArrayList<String> IAmFinished(ArrayList<File> list) {
for(int i=0;i< list.size();i++){
name.add(list.get(i).getName());
}
return name;
}
private class Longoperation extends AsyncTask<File, Void, ArrayList<File>>{
File dir;
public IAsyncTask asyncTaskListener;
public Longoperation(File dir,IAsyncTask asyncTaskListener) {
this.dir = dir;
this.asyncTaskListener = asyncTaskListener;
}
@Override
protected ArrayList<File> doInBackground(File... params) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
doInBackground(listFile[i]);
} else {
boolean booleanpdf = false;
if (listFile[i].getName().endsWith(".zip") || listFile[i].getName().endsWith(".jar")) {
for (int j = 0; j < fileList.size(); j++) {
if (fileList.get(j).getName().equals(listFile[i].getName())) {
booleanpdf = true;
} else {
}
}
if (booleanpdf) {
booleanpdf = false;
} else {
fileList.add(listFile[i]);
}
}
}
}
}
return fileList;
}
public void onPostExecute(ArrayList<File> list) {
asyncTaskListener.IAmFinished(list);
}
}
}
答案 0 :(得分:1)
您无法使用longoperation(dir1, asyncTask1)
。对象不是函数,因此您不能像它们一样使用它。
将以下代码Longoperation longoperation = new Longoperation(dir1,this);
更改为Longoperation longoperation
。然后将longoperation(dir1, asyncTask1).execute();
更改为longoperation = new Longoperation(dir1, this);