从Asynctask访问UI元素

时间:2014-06-07 18:13:23

标签: android android-asynctask androidplot

我和Asynctask稍微挣扎,我需要一些帮助。

我正在使用Androidplot绘制一些图表,因为它非常慢,我想使用Asynctask来更快地完成它。

问题是我不知道如何从Asynctask类访问我在onCreate()中声明的元素。 这是代码:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.waveform);

     plot = (XYPlot) findViewById(R.id.XYPlot0);
        widget = plot.getGraphWidget();
        plot2= (XYPlot) findViewById(R.id.XYPlot01);
        widget2=plot2.getGraphWidget();
        plot3= (XYPlot) findViewById(R.id.XYPlot02);
        widget3=plot3.getGraphWidget();


        Operaciones2 ope=new Operaciones2();
        ope.execute();

     // Turn the above arrays into XYSeries':
        XYSeries series1 = new SimpleXYSeries(
                Arrays.asList(n),          // SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                "Series1");   

        XYSeries series2 = new SimpleXYSeries(
                Arrays.asList(l),          // SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                "Series1"); 

         XYSeries series3 = new SimpleXYSeries(
                Arrays.asList(g2),          // SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                "Series1");



        // Create a formatter to use for drawing a series using LineAndPointRenderer
        // and configure it from xml:
        LineAndPointFormatter series1Format = new LineAndPointFormatter();
        //series1Format.setLinePaint(paint);
        series1Format.setPointLabelFormatter( (PointLabelFormatter) null); // con esto en null borramos el valor de las muestras de encima de los puntitos
        series1Format.getVertexPaint().setStrokeWidth(1); //Establece el tamaño de los puntos de cada muestra
        Paint linePaint=new Paint();
        Paint verPaint=new Paint();
        switch(color){
        case Color.BLUE: 
            //linePaint.setColor(Color.BLUE);
            //verPaint.setColor(this.getResources().getColor(R.color.DarkBlue));
            series1Format.configure(getApplicationContext(),R.xml.line_blue);
        break;
        case Color.RED:
            //linePaint.setColor(Color.RED);
            //verPaint.setColor(this.getResources().getColor(R.color.DarkRed));
            series1Format.configure(getApplicationContext(),R.xml.line_red);
        break;
        case Color.GREEN:
            //linePaint.setColor(Color.GREEN);
            //verPaint.setColor(this.getResources().getColor(R.color.DarkGreen));
            series1Format.configure(getApplicationContext(),R.xml.line_point_formatter_with_plf1);
        break;
        }
        int i;

然后我在这里有Asynctask代码:

  private class Operaciones2 extends AsyncTask<Void, Integer, Void> {
    int progress;   
    double [] st =new double [arr.length];
    int j=0;


    protected void onPostExecute(Void... unused ){
        plot.addSeries(series1, series1Format);
        plot2.addSeries(series2, series1Format);
        plot3.addSeries(series3, series1Format);
        Toast.makeText(getApplicationContext(), "Todo cargado", Toast.LENGTH_LONG).show();
    }

    protected Void doInBackground(Void... unused){


            for (int i = 44; i < s; i+=2) {
                // convert byte pair to int
                double audioSample = (double) (array[i+1] << 8 | array[i] & 0xff)/ 32767.0;

                arr[j]=audioSample;  //double
                n[j] = (Number)arr[j];  //Number

                st[j]=10*Math.log10(Math.abs((audioSample/Math.pow(10, -12)))); //double
                l[j]=(Number)(10*Math.log10(Math.abs((audioSample/Math.pow(10, -12)))));  //Number

                if(audioSample == 0.0 ){
                    if(j!=0){
                    l[j]=l[j-1];
                    st[j]=st[j-1];
                    }else{
                        l[j]=0.0;
                        st[j]=0.0;
                    }
                  }

                j=j+1;}
                min=Operaciones.minimum(arr);
                max=Operaciones.maximum(arr);
                min2=Operaciones.minimum(st);
                max2=Operaciones.maximum(st);
                /*****************************/
                arreaFFT();
            return null;
    }
 }

我的想法是将在doInBackground中计算的系列添加到onPostExecute中,但问题是我真的不知道如何从AsyncTask访问onCreate()中创建的series1-2-3和series1Format(它说是它们无法解析为变量。)

任何想法??

3 个答案:

答案 0 :(得分:1)

只需在AsyncTask构造函数中提供回调即可。这通常是Activity(或Fragment)本身,实现特定的接口。

请参阅Android, can I put AsyncTask in a separate class and have a callback?

这是AsyncTask的常见模式。

答案 1 :(得分:0)

您只能在onProgressUpdate的{​​{1}}和onPostexecute方法中进行用户界面更改。

答案 2 :(得分:0)

其他选项是将您的series1Formatplot声明为该班级的成员,然后您可以从onPostExecute

访问它
public myActivity extends Activity{
    LineAndPointFormatter mSeries1Format;
    // Declare plot as member


    protected void onCreate(Bundle savedInstanceState) {
        // Initialize mSeries1Format
    }      

}