从AsyncTask的访问UI元素 [英] Access UI elements from Asynctask

查看:266
本文介绍了从AsyncTask的访问UI元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林挣扎AsyncTask的一点点,我需要一些帮助。

Im struggling a little bit with Asynctask and I would need some help.

我绘制一些图表与Androidplot和,因为它是极其缓慢的,我想用的AsyncTask做得更快。

I am plotting some graphs with Androidplot and, as it is extremely slow, I want to use Asynctask to do it faster.

问题是,我不知道如何访问到)从AsyncTask的类,我的onCreate(已宣布的元素。
这里是code:

The problem is that I don´t know how to access to the elements that I have declared in onCreate() from the Asynctask class. Here is the code:

 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的code位置:

Then I have the Asynctask code here:

  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系列,但问题是,我不真的知道如何访问到series1-2-3和)从AsyncTask的中的onCreate(创建series1Format(它说,它们不能被解析为一个变量)。

My idea is to add the series calculated at doInBackground into onPostExecute, but the problem is that I don´t really know how to access to series1-2-3 and series1Format created in onCreate() from the AsyncTask (it says that they cannot be resolved to a variable).

任何想法??

推荐答案

另一个选项是宣布你的 series1Format 剧情作为类中的一员,那么你可以从 onPostExecute

Other option would be declaring your series1Format and plot as a member of the class, then you could access it from onPostExecute

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


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

}

这篇关于从AsyncTask的访问UI元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆