Access UI elements from Asynctask

无人久伴 提交于 2019-12-11 21:48:10

问题


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??


回答1:


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
    }      

}



回答2:


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.




回答3:


You can make UI changes only in onProgressUpdate and onPostexecute methods of AsyncTask.



来源:https://stackoverflow.com/questions/24100236/access-ui-elements-from-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!