0

I receive a double array from extras, then I need to do some things on background so I send double array to an asynctask. At this point I get an syntax error, so I would like like to now how to pass a double array (confparams) to asyntask.

My code:

final double confparams[]= extras.getDoubleArray("confparams");

AlertDialog.Builder builder = new AlertDialog.Builder(a_2_Att_Eleccion.this);
                        builder.setMessage(msg_calc)
                                .setTitle(R.string.a_2_dialWhatCalculateTitle)
                                .setIcon(android.R.drawable.ic_dialog_alert)
                                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {

                                    new Calculate_asyntask().execute(confparams);
                                }
                            });

                    builder.setNeutralButton("CANCEL",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User clicked cancel button
                            return;
                        }
                    });

....................

public class Calculate_asyntask extends AsyncTask<double[], Integer, double[]> {

    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = ProgressDialog.show(a_2_Att_Eleccion.this, getString(R.string.a_2_dialCalcTitle),getString(R.string.a_2_dialCalcMessage));
    }

    @Override
    protected double[] doInBackground(double ... params) {

        double final_result[] = new double[0]; 

        if(result_sci[0]!=null){

            a_3_CalcScill scill=new a_3_CalcScill();
            sci=scill.CalcScill(result_sci,params,getApplicationContext());
            final_result=sci;

        }

        return final_result;
    }

So how can I send a doubleArray to an Asyntask?

The syntax double[] I used for asynctask for result works, but why not on the definition of asyntask and doInbackground for params?

Thanks in advance!

Txispas
  • 159
  • 4
  • 14

3 Answers3

0

change as following:

@Override
protected double[] doInBackground(double[] ... params) {

    double final_result[] = new double[0]; 

    if(result_sci[0]!=null){

        a_3_CalcScill scill=new a_3_CalcScill();
        sci=scill.CalcScill(result_sci,params,getApplicationContext());
        final_result=sci;

    }
    return final_result;
}
madteapot
  • 2,208
  • 2
  • 19
  • 32
0

because your doInBackground is just a double parameter you need double[]

protected double[] doInBackground(double[] ... params) {

to get you data in the doInBackground you need to do this

double[] data = params[0];

you could also have done this

private double[] foo;

public Calculate_asyntask(double[] params){
    foo = params;
}

then

new Calculate_asyntask(confparams).execute();

would do it

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • Hi, no if I put doInBackground(double[] ... params), then told me that params is a bidimensional array, as params[][]. So i Think that has to be only (double...params). Maybe because you say that is a protected double[] doInBackground. So still persist the error. – Txispas May 29 '14 at 21:04
  • if you pass parameters in the execute line you would have to get the actual data by doing `params[0]` and that gets the data you sent in – tyczj May 29 '14 at 21:07
  • @Txispas have a look at this question for the reason http://stackoverflow.com/questions/15236541/purpose-of-asynctask-varargs-parameters – tyczj May 29 '14 at 21:13
  • I do not why but If I put on execute a String array an then in input I put and in doInbackground (String...params) I receive an array... Why can I do it with double[]? that´s a pitty – Txispas May 29 '14 at 21:35
0

Define the AsyncTask as follows...

public class Calculate_asyntask extends AsyncTask<Double, Integer, Double[]>

Also change the doInBackground declaration to...

protected Double[] doInBackground(Double ... params)

In your doInBackground method, params will be of type Double[] and you can access each array element as params[x] (for example) or simply pass it on as it is.

Squonk
  • 48,735
  • 19
  • 103
  • 135