OnPostExecute无法正常工作,无法绘制图形

时间:2011-12-21 06:45:13

标签: java android android-activity android-asynctask

onButtonClcick按钮index,应显示一个graph,用于从网址获取数据。我已将AsyncTask用于此目的。

我被困在两种方法之间。

1)如果我在afterload()点击postExecute()按钮时调用index方法,我只是获得第一个屏幕截图,如下所示,即 GRAPH IS NOT获得显示

我对其进行了调试,发现调用了afterload()方法,但仍未显示图形。

2)如果我没有在afterload()中拨打postExecute()方法。相反,我将afterload()方法的所有内容复制到onCreate()方法新类graphView,然后图表正在显示。如第二张截图所示

所有其他功能类似于过滤器,过滤列表数据丢失。 (请注意,第二个屏幕截图表未显示)。这是因为在第二个活动中使用setContentView()因此布局重新启动,因此控制将进入第二个活动。

任何帮助都将有益于生命!

DataAttach.java

    public class DataAttach extends Activity {
        //ALL VARIABLES DECLARED

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            init();
        }

        public void init() {
            //ALL FINDBYVIEWID COMMANDS

            DownloadWebPageTask task = new DownloadWebPageTask();
            task.execute(new String[] { "http://abc.com/" });

                ed.addTextChangedListener(new TextWatcher() {

                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {
                        //filtering listview data
                    }
                });

                lsym.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {

                    }
                });

                btnindex.setOnClickListener(new Button.OnClickListener() {
                    public void onClick(View v) {

                        mySimpleXYPlot.setVisibility(View.VISIBLE);
                        WebPageTask task1 = new WebPageTask();
                        task1.execute(new String[] { "http://pqr.com/" });
                    }
                });

                btngainer.setOnClickListener(new Button.OnClickListener() {
                    public void onClick(View v) {
                        DownloadWebPageTask task = new DownloadWebPageTask();
                        task.execute(new String[] { "http://xyz.com/" });
                    }
                });

                btnloser.setOnClickListener(new Button.OnClickListener() {
                    public void onClick(View v) {
                        DownloadWebPageTask task = new DownloadWebPageTask();
                        task.execute(new String[] { "http://ijk.com/" });
                    }
                });

            finally {
                dbM.close();
            }
        }

        private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
            //DO SOMETHING
            }

            @Override
            protected void onPostExecute(String result) {
                //DO SOMETHING
            }
        }

        private class WebPageTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... urls) {
                   //FETCHING DATA FROM URL
            }

            @Override
            protected void onPostExecute(String result) {

                //POPULATING 'time' & 'current' ARRAYS

                afterload(time, current);
  //============================================================================
  //============================================================================
 //IF I USE BELOW COMMENTED CODE THEN IT DISPLAYS GRAPH BUT ALL THE ONCLICKLISTENERS DOESN'T WORK.
  //============================================================================
  //============================================================================
                // Bundle b = new Bundle();
                // b.putStringArray("time", time);
                // b.putStringArray("current", current);
                //
                // Intent i = new Intent(getApplicationContext(), DrawGraph.class);
                // i.putExtras(b);
                // DataAttach.this.startActivity(i);
            }

            public void afterload(String[] time, String[] current) {

            //POPULATING 'timenumber' & 'currentnumber' ARRAYS

                Number[] series1Numbers = timenumber;
                Number[] series2Numbers = currentnumber;

                XYSeries series1 = new SimpleXYSeries(
                        Arrays.asList(series1Numbers),
                        Arrays.asList(series2Numbers), "Reliance");

                LineAndPointFormatter series1Format = new LineAndPointFormatter(
                        Color.rgb(0, 200, 0), // line color
                        Color.rgb(0, 100, 0), // point color
                        Color.rgb(150, 190, 150)); // fill color (optional)

                mySimpleXYPlot.addSeries(series1, series1Format);
                mySimpleXYPlot.setTicksPerRangeLabel(2);
                mySimpleXYPlot.setTicksPerDomainLabel(2);
                mySimpleXYPlot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 1);
                mySimpleXYPlot.setDomainStep(XYStepMode.SUBDIVIDE, 10);
                mySimpleXYPlot.setDomainValueFormat(new MyDateFormat());
                mySimpleXYPlot.disableAllMarkup();
            }
        }

        public class MyDateFormat extends Format {
            //CONVERTING DATE FORMAT
    }

1ST&amp;之前采取了第2次屏幕曝光点击INDEX按钮后

SCREENSHOT

1 个答案:

答案 0 :(得分:0)

在afterload()方法结束时使用 mySimpleXYPlot.redraw() mySimpleXYPlot.postRedraw()

如果您看到来源:重绘()方法会使视图无效,因此您的数据将会显示。