我创建了这个在doInBackground
中无法正常工作的AsyncTask。为了更好地理解,我指出了一些事情:
getFunctionVal(String function, double Val);
此方法返回以点计算的函数的值(例如:getFunctionVal("x+3", 2.0)
= 5.0)。funzione
和derivata
是两个字符串(它们是公式)。 问题:我无法理解为什么soluzioni.size()
始终为0,risultati.size()
也会发生同样的事情。当我在UI中的OnClickListener
内部使用此代码时,这并没有发生。
public double b = -20;
public double a = -20;
public class TaskAsincrono extends AsyncTask<Void, Void, Boolean> {
private Context mContext;
private View view;
public List<Double> risultati = new ArrayList<>();
public List<Double> soluzioni = new ArrayList<>();
private ProgressDialog dialog;
public TaskAsincrono (Context context, View v){
mContext = context;
view = v;
}
@Override
protected void onPreExecute(){
dialog = ProgressDialog.show(view.getContext(), "", "Attendi...", false, true);
}
@Override
protected void onProgressUpdate(Void[] values) {
};
@Override
protected Boolean doInBackground(Void... params) {
int step = 500;
double k = (b-a)/step;
List<Double> valoriIntervallo = new ArrayList<>();
List<Double> valoriFunzione = new ArrayList<>();
//Generates number for intervals
for(int i = 0; i < step; i++) {
double ak = a + k;
double fak = getFunctionVal(funzione, ak);
a += k;
valoriIntervallo.add(ak);
valoriFunzione.add(fak);
}
//show the number intervals
for(int j = 0; j < valoriIntervallo.size()-1; j++) {
if (Math.signum(valoriFunzione.get(j)) != Math.signum(valoriFunzione.get(j+1))) {
risultati.add(valoriIntervallo.get(j));
risultati.add(valoriIntervallo.get(j+1));
}
}
k = 0;
for (int i = 0; i < (risultati.size()); i += 2) {
k++;
double a = risultati.get(i);
double b = risultati.get(i + 1);
double res = (b + a) / 2;
for (int j = 1; j < 10; j++) {
res = res - (getFunctionVal(funzione, res) / getFunctionVal(derivata, res));
}
soluzioni.add(res);
}
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
if(dialog.isShowing()) { dialog.dismiss(); }
TableLayout tl = (TableLayout) view.findViewById(R.id.tableLayoutTangenti);
for (int o = 0; o < 1; o++) {
TableRow tr = new TableRow(mContext);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView coefficente = new TextView(mContext);
coefficente.setText("x" + String.valueOf((o+1)) + " = ");
coefficente.setGravity(Gravity.CENTER);
coefficente.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.2f));
EditText decimale = new EditText(mContext);
decimale.setGravity(Gravity.CENTER);
decimale.setText(String.valueOf(risultati.size()));
decimale.setFocusable(false);
decimale.setFocusableInTouchMode(false);
decimale.setClickable(false);
decimale.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.8f));
tr.addView(coefficente);
tr.addView(decimale);
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
}
@Override
protected void onCancelled() {
mAuthTask = null;
}
}
如果你问我为什么要使用AsyncTask,那是因为我需要step = 50000
而我不能强调#34;用户界面这么多。
答案 0 :(得分:2)
检查条件 if(Math.signum(valoriFunzione.get(j))!= Math.signum(valoriFunzione.get(j + 1)))评估为真。
答案 1 :(得分:1)
k
为0. funzione
是常量。假设getFunctionVal
是纯函数,valoriFunzione
包含相同值的500倍。
这意味着Math.signum(valoriFunzione.get(j)) != Math.signum(valoriFunzione.get(j+1))
始终为假。
这就是risultati
中没有任何内容的原因。