Issue
I am trying to return value from Asynctask by creating an interface but while executing Asynctask class , its giving me method call expected error .
I am getting the error in line longoperation(dir1, asyncTask1).execute();
How do I fix it ?
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);
}
}
}
Solution
You can't use longoperation(dir1, asyncTask1)
as you are using. Objects are not functions, so you can't use it like them.
Change the following code Longoperation longoperation = new Longoperation(dir1,this);
to Longoperation longoperation
. Then change longoperation(dir1, asyncTask1).execute();
to longoperation = new Longoperation(dir1, this);
Answered By - Rohan Kandwal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.