我有一个AsyncTask类作为片段的内部类。
public class MyAsyncTask extends AsyncTask<String,double[],double[]> {
double posScoreb = 0.0;
double negScoreb= 0.0;
public AsyncResponse delegate = null;
@Override
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);}
@Override
protected double[] doInBackground(String... params) {
s = new SentiCalc(getActivity().getAssets());
s.setInput(String.valueOf(params[0]));
double[] set = new double[]{};
try {
set = s.getSenti();
} catch (IOException e) {
e.printStackTrace();
}
posScoreb = set[1];
negScoreb = set[2];
return new double[]{posScoreb,negScoreb};
}
@Override
protected void onPostExecute(double[] result) {
progressBar.setVisibility(View.GONE);
result[0] = posScoreb;
result[1] = negScoreb;
delegate.processFinish(result);
Toast.makeText(getActivity(), "Done",Toast.LENGTH_LONG).show();
}
}
我从同一片段中的方法saveNote()调用它。
asyncTask = new MyAsyncTask() ;
asyncTask.delegate =this;
asyncTask.execute(content);
我希望AsyncTask类将结果(double数组)中的值返回到saveNote方法。如图所示,如何将OnPostExecute()的结果导入主活动,因为AsyncTask是一个单独的类? 我创建了一个接口并创建了我的片段类来实现它。
public interface AsyncResponse {
void processFinish(double[] output);}
因此我可以在方法processFinish中获得结果 在同一个片段中。
@Override
public void processFinish(double[] output) {
double posScore,negScore;
posScore = output[0];
negScore = output[1];
Toast.makeText(getActivity(),String.valueOf(posScore),Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), String.valueOf(negScore), Toast.LENGTH_LONG).show();
}
如何在我调用AsyncTask类的saveNote方法中获取此结果?