I have create a class asyncTask inside a FragmentActivity class:
class aysncTask extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... params) {
assert(filterfiles != null);
while( !searchDone.getValue() )
{
filterfiles.searchTargetFile(android.os.Environment.getExternalStorageDirectory()+ "/");
publishProgress(); //Doesn't work here.
}
return null;
}
@Override
protected void onProgressUpdate(Void... params)
{
UpdateUI(); //Only executes as the search is done.
}
@Override
protected void onPostExecute(Void result)
{
}
}
filterfiles.searchTargetFile(~) function searches the local data, adding them to an ArrayList: cachedData. But I can't wait for the searching is done and then update the UI. The user will wait too long. But I don't know where to put the updating UI code. It can't be inside the method onPostExecute(Void result), because I still have to wait the searching is done.
It can't also be inside the method onProgressUpdate(Void params), because once I call filterfiles.searchTargetFile(~), it will search the matched files in local system. Only when the search is done, it will call publishProgress().
What comes into my mind is creating another thread observing the cachedData, once a file is found, I will add the file and updating the UI.
But I don't know how to do? Is this a good practice? Please provide some suggested solution! I have spend two days in this issue.
0 comments:
Post a Comment