DK64 DK64 - 4 months ago 12
Android Question

ArrayList not working in AsyncTask

I have created this AsyncTask that is not properly working in

doInBackground
. To better understand, I am pointing out some things:


  1. getFunctionVal(String function, double Val);
    this method returns the value of a function calculated in a point (Ex.:
    getFunctionVal("x+3", 2.0)
    = 5.0).

  2. funzione
    and
    derivata
    are two strings (they are equations).



Problem: I cannot understand why
soluzioni.size()
is always 0, and the same thing happens with
risultati.size()
. This didn't happen when I had this code inside a
OnClickListener
in the UI.

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;
}
}


If you are asking why I am using an AsyncTask, it's because I need to have
step = 50000
and I cannot "stress" the UI so much.

Answer

k is 0. funzione is constant. Assuming getFunctionVal is a pure function, valoriFunzione contains 500 times the same value.

Which means that Math.signum(valoriFunzione.get(j)) != Math.signum(valoriFunzione.get(j+1)) is always false.

Which is why there is nothing in risultati.

Comments