在下一个代码上重复AsyncTask

时间:2014-02-23 22:12:46

标签: android android-asynctask refresh

我有一个代码只接收来自网络的信息Jsoup,我想每秒刷新这些信息。我尝试使用我在Google和stackoverflow中找到的所有代码,但没有运气。非常感谢你提前。 [解决]

现在我正在尝试使用Bundle和Intent将一个数组从MainActivity发送到另一个名为“Activity_allday”的Activity,当调用viewallday()函数时按下“btviewallday”按钮但没有运气。有什么建议吗?

LogCat错误:无法在视图类android.widget.Button上的onClick处理程序的活动类com.example.Chispa.MainActivity中找到方法viewallday(View),其id为“btviewallday”。

我注意到错误来自于在viewallday接收两个值(View view,Pair p)。如何在viewallday函数中接收“Pair p”值?

以下是新的应用代码:

[MainActivity]

public class MainActivity extends Activity {

private TextView tvmax, tvmid, tvmin, tvactualval,tvvaloractual,tvdate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvdate=(TextView)findViewById(R.id.tvdate);
        tvvaloractual=(TextView)findViewById(R.id.tvvaloractual);
        tvmax=(TextView)findViewById(R.id.tvmaximo);
        tvmid=(TextView)findViewById(R.id.tvmedio);
        tvmin=(TextView)findViewById(R.id.tvminimo);

        new BackGroundTask().execute();

        callAsynchronousTask();

    }


    public void callAsynchronousTask() {
        final Handler handler = new Handler();
        Timer timer = new Timer();
        TimerTask doAsynchronousTask = new TimerTask() {       
            @Override
            public void run() {
                handler.post(new Runnable() {
                    public void run() {       
                        try {
                            BackGroundTask performBackgroundTask = new BackGroundTask();
                            // PerformBackgroundTask this class is the class that extends AsynchTask 
                            performBackgroundTask.execute();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                        }
                    }
                });
            }
        };
        timer.schedule(doAsynchronousTask, 0, 1000); //execute in every 1000 ms
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public class Pair
    {
        public String[] bar;
        public String[] values;
    }



    public void viewallday(View view, Pair p) {

        Intent intent = new Intent(this, Activity_allday.class);

        Bundle bundle =new Bundle();
        bundle.putStringArray("bar", p.bar);
        intent.putExtras(bundle);
        startActivity(intent);

    }



    class BackGroundTask extends AsyncTask<Void, Void, Pair> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }


        public String[] getValuesGraph(Document doc) {


                int cont=24,var=7;
                String bar[] = new String[cont];



               /*
                * Getting elements from the graphic in an array from 0-23. 0 it's 1:00am, 23 it's 00:00am
                */

               for (cont=0; cont < 24; cont++){

               String onMouseOver = doc.select("a").get(var+cont).attr("onMouseOver");
               bar[cont] = onMouseOver.split("'")[9];


               }


               return bar;



        }



        public String[] getValuesFooter(Document doc) {



            String values[] = new String[7];


            /*
                * Getting elements from the graphic footer
                */

               String delimiters= "[ /]+";
               Elements elements = doc.select("td.cabeceraRutaTexto");
               elements.size(); // 6



                   /*  Getting text from table */
                   values[0] = elements.get(0).text(); // TITLE
                   values[1] = elements.get(1).text(); // TEXT MAX VALUE
                   values[2] = elements.get(2).text(); // TEXT MIDDLE VALUE
                   values[3] = elements.get(3).text(); // TEXTO MIN VALUE

                   /* Getting numbers from table */

                   values[4] = elements.get(4).text().split(delimiters)[0]; // NUMBER MAX VALUE
                   values[5] = elements.get(5).text().split(delimiters)[0]; // NUMBER MIDDLE VALUE
                   values[6] = elements.get(6).text().split(delimiters)[0]; // NUMBER MIN VALUE    


                   return values;


         }




        @Override
        public Pair doInBackground(Void... params) {

            Pair p = new Pair();


            try {

                URL url= new URL("http://www.myweb.com");
                Document doc = Jsoup.connect(url.toString()).get();

                p.bar = getValuesGraph(doc);

                p.values = getValuesFooter(doc);

               /*
                * Getting elements from the graphic in an array from 0-23. 0 it's 1:00am, 23 it's 00:00am
                */


         return p;

            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }



        public String ActualHourValue() {

            Format formatter = new SimpleDateFormat("H");
            String onlyhour = formatter.format(new Date());

            return onlyhour;


        }

        public void ShowDateHour(){


            Calendar c = Calendar.getInstance();
            SimpleDateFormat df3 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a");
            String formattedDate3 = df3.format(c.getTime());
            tvdate.setText("Fecha y hora actuales : "+formattedDate3);

        }


        @Override
        protected void onPostExecute(Pair p) {


            int hour =  Integer.parseInt(ActualHourValue());
            tvvaloractual.setText(p.bar[hour]+" €/MWh");

            tvmax.setText(p.values[4]+" €/MWh");
            tvmid.setText(p.values[5]+" €/MWh");
            tvmin.setText(p.values[6]+" €/MWh");

            ShowDateHour();

           /*super.onPostExecute(p.values);*/
        }




    }




}

[Activity_allday]

Public class Activity_allday extends MainActivity {

private TextView tvall;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_day_prices);

    tvall = (TextView) findViewById(R.id.tvall);

    Bundle bundle = this.getIntent().getExtras();
String[] bar=bundle.getStringArray("bar");
    /*tvall.setText(bar[0]);*/
}

public void back (View view) {
    finish();
}

}

0 个答案:

没有答案