Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
502 views
in Technique[技术] by (71.8m points)

android - Access UI elements from Asynctask

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

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

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;

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

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).

Any ideas??

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just provide a callback in the AsyncTask constructor. This is generally the Activity (or Fragment) itself, implementing a particular interface.

See Android, can I put AsyncTask in a separate class and have a callback?

This is a common pattern with AsyncTask.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...