将double []发送到Asynctask

时间:2014-05-29 20:48:46

标签: android android-asynctask double

我从extras收到一个双数组,然后我需要在背景上做一些事情,所以我发送双数组到asynctask。此时我收到语法错误,所以我想现在如何将双数组(confparams)传递给asyntask。

我的代码:

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

那么如何将doubleArray发送到Asyntask呢?

语法double []我用于结果的asynctask工作,但为什么不为params的asyntask和doInbackground的定义?

提前致谢!

3 个答案:

答案 0 :(得分:0)

更改如下:

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

答案 1 :(得分:0)

因为你的doInBackground只是一个double参数,你需要double[]

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

在doInBackground中获取数据,你需要这样做

double[] data = params[0];

你也可以这样做

private double[] foo;

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

然后

new Calculate_asyntask(confparams).execute();

会这样做

答案 2 :(得分:0)

AsyncTask定义如下......

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

同时将doInBackground声明更改为...

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

doInBackground方法中,参数类型为Double[],您可以将每个数组元素作为params[x](例如)访问,或者只是按原样传递它。