我已經創建了這個在doInBackground
中未正確工作的AsyncTask。爲了更好地理解,我指出了一些事情:ArrayList在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
我做不到「壓力」的UI這麼多。
爲什麼需要在AsyncTask中?什麼'getFunctionVal'這樣做需要它在後臺線程? –
你確定步驟變量不是0嗎?我遵循邏輯,這是我想出的最合乎邏輯的結論 –
這並不意味着冒犯或成爲smarta **,但在您的代碼中使用英語會極大地提高每個不說話的人的可讀性意大利。 – FWeigl