创建片段和asynctask

时间:2014-04-22 17:29:01

标签: java android android-asynctask

大家好我有一个大问题...... 我有两个asynctask ...

arrivi.java:

    public class arrivi extends Fragment implements InterfaceTaskFinished{
    ListView list;
    int thread = 0;
    List<RowItem> rowItems;

    public String[] lista = new String[200];
    public String[] Compagnia = new String[200];
    public String[] CodiceVolo = new String[200];
    public String[] Citta = new String[200];
    public String[] OraPrevista = new String[200];
    public String[] OraStimata = new String[200];
    public String[] StatoVolo = new String[200];


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View arrivi = inflater.inflate(R.layout.arrivi, container, false); 

          new Arrivi(this).execute();
       // new partenze().new Partenze().execute();

       return arrivi;
    }

    public class Arrivi extends AsyncTask<String, Integer, Boolean> {
        ProgressDialog prog;
    //    String info;
        boolean result;
        private final InterfaceTaskFinished listener;

        public Arrivi(InterfaceTaskFinished listener) {
            this.listener = listener;
        }

        @Override
        protected void onPreExecute() {
            prog = new ProgressDialog(getActivity());
            prog.setMessage("Connessione in corso...");
            prog.show();
            prog.setCanceledOnTouchOutside(false);
            prog.setCancelable(false);


        }

        @Override
        protected Boolean doInBackground(String... params) {
            try {           
                org.jsoup.nodes.Document doc = Jsoup.connect("http://s.eu").timeout(7*1000).get();

                org.jsoup.nodes.Element tabella = doc.getElementsByClass("tabella-voli").first();
                Iterator<org.jsoup.nodes.Element> iterator = tabella.select("td").iterator();

                while(iterator.hasNext()){
                    thread++;

                    Compagnia[thread] = iterator.next().text();
                    CodiceVolo[thread] = iterator.next().text();
                    Citta[thread] = iterator.next().text();
                    OraPrevista[thread] = iterator.next().text();
                    OraStimata[thread] = iterator.next().text();
                    StatoVolo[thread] = iterator.next().text();
                    System.out.println(StatoVolo[thread]);
                }
            }catch (IOException e) {
                e.printStackTrace();
            }return true;
        }

        @Override
        protected void onPostExecute(Boolean result) { 
             if(!result) {
                 return;
             }

             if(listener != null) {
                 prog.dismiss();
               CheckRow();

                 listener.onTaskFinished();
             }
         }


    }// FINE THREAD 

    public void CheckRow(){

        rowItems = new ArrayList<RowItem>();
        for (int i = 1; i <= thread; i++) {

            RowItem item = new RowItem(Compagnia[i], CodiceVolo[i], Citta[i],OraPrevista[i],  OraStimata[i], StatoVolo[i]);
            rowItems.add(item);
        } 
        list=(ListView)getActivity().findViewById(R.id.listView1);
        CustomBaseAdapter adapter = new CustomBaseAdapter(getActivity(), rowItems);
        list.setAdapter(adapter);
    }

    @Override
    public void onTaskFinished() {
        // TODO Auto-generated method stub
            new partenze();
            new partenze().new Partenze().execute();
    }
}  

partenze.java:

    public class partenze extends Fragment {

    ListView list;
    int thread = 0;
    List<RowItem> rowItems;

    public String[] lista = new String[200];
    public String[] Compagnia = new String[200];
    public String[] CodiceVolo = new String[200];
    public String[] Citta = new String[200];
    public String[] OraPrevista = new String[200];
    public String[] OraStimata = new String[200];
    public String[] StatoVolo = new String[200];

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View partenze = inflater.inflate(R.layout.partenze, container, false);
      // new Partenze().execute("");
          return partenze;
   }

   public class Partenze extends AsyncTask<String, Void, String> {
       ProgressDialog prog;
       String info;

       @Override
       protected void onPreExecute() {

       }

       @Override
       protected String doInBackground(String... params) {
        try {       
               org.jsoup.nodes.Document doc = Jsoup.connect("http://s.eu").timeout(7*1000).get();

               org.jsoup.nodes.Element tabella = doc.getElementsByClass("tabella-voli").get(1);
               Iterator<org.jsoup.nodes.Element> iterator = tabella.select("td").iterator();


               while(iterator.hasNext()){ 
                   thread++;

                   Compagnia[thread] = iterator.next().text();
                   CodiceVolo[thread] = iterator.next().text();
                   Citta[thread] = iterator.next().text();
                   OraPrevista[thread] = iterator.next().text();
                   OraStimata[thread] = iterator.next().text();
                   StatoVolo[thread] = iterator.next().text();
               }
        }catch (IOException e) {
               e.printStackTrace();
        }return info;
       }

       @Override
       protected void onPostExecute(String result) { 
           super.onPostExecute(result);
         //  prog.dismiss();

           CheckRow();
       }
   }// FINE THREAD 


   public void CheckRow(){

    rowItems = new ArrayList<RowItem>();
       for (int i = 1; i <= thread; i++) {

           RowItem item = new RowItem(Compagnia[i], CodiceVolo[i], Citta[i],OraPrevista[i],  OraStimata[i], StatoVolo[i]);
           rowItems.add(item);
       } 
       list=(ListView)getActivity().findViewById(R.id.listView2);
       CustomBaseAdapter adapter = new CustomBaseAdapter(getActivity(), rowItems);
       list.setAdapter(adapter);


   }
}

错误是:

04-22 19:19:20.860: E/AndroidRuntime(23765):    at com.gianni.app.partenze.CheckRow(partenze.java:92)

它是:

   list=(ListView)getActivity().findViewById(R.id.listView2);

我需要为wiew创建...有一个执行第二个线程的解决方案

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View partenze = inflater.inflate(R.layout.partenze, container, false);
          return partenze;
   }

???? 然后doInBackground ......等...... 谢谢!!

2 个答案:

答案 0 :(得分:0)

这个问题非常不清楚,但我会抓住你想要做的事情。

如果你真的需要做这样的多个任务,你真的应该使用Service来在后台处理这些,特别是因为你有一些交叉代码。

如果你有需要在自己的线程中完成的任务,但是有序的你应该考虑旋转新的Runnable线程,然后创建一个情况,其中下一个无法启动而不被另一个线程旋转,然后自己关闭。

旋转新线程:

new Thread(new ConnectThread()).start();

Runnable线程类:

class ConnectThread implements Runnable {
    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
                   //Do Stuff 
                   if(finish){ 
                       //stop the thread and start the other passing variables
                       //Use this to stop the thread
                       Thread.currentThread().interrupt();
                    } 
}

AsyncTask应该只在您需要只有该Activity需要的信息时使用,并且它还会占用一个Activity可以使用的资源。使用通用Service类来处理切换更容易,性能更高。

这有帮助吗?

答案 1 :(得分:0)

04-22 19:19:20.860: E/AndroidRuntime(23765):    at com.gianni.app.partenze.CheckRow(partenze.java:92)

这只是日志输出中的一行,它告诉您错误发生的部分,而不是实际发生的情况。您需要显示更多的堆栈跟踪。

list=(ListView)getActivity().findViewById(R.id.listView2);

将此更改为致电

list = (ListView) getView().findViewById(R.id.listView2);

如果这不起作用,那么您的XML可能有问题(可能它没有返回列表视图)。发布XML。