2014-12-08 48 views
0

我不明白我是否做過處理? 我有一個類需要參考輸入和輸出給我一個對象文檔(Jsoup庫)。然後我在主流中解析對象並設置表單數據。在工作開始時,我展示了ProgressDialog,並在最後展示了它。但一切正常,不清楚。我運行一個進程asinktask開始,表單凍結(大約一秒鐘)。補充工具沒有時間關閉。然後窗體下降ProgressDialog出現一瞬間並關閉。數據被安裝在表單上。一切都非常快,但是什麼形式凍結和ProgressDialog只有在它下降後纔開始擔心我。這個問題上面說我不能用的AsyncTask

public class MyAsincTask extends AsyncTask<String, Void, Document> { 
    private Document document; 
    private ProgressDialog progressDialog; 

    public MyAsincTask(Context context) { 
     progressDialog = MyProgress.getProgressDialog(context); 
    } 

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

    @Override 
    protected Document doInBackground(String... params) { 
     try { 
      document = Jsoup.connect(params[0]).get(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return document; 
    } 

    @Override 
    protected void onPostExecute(Document document) { 
     progressDialog.cancel(); 
     super.onPostExecute(document); 
    } 
} 

public void setData(){ 
      MyAsincTask task = new MyAsincTask(getActivity()); 
      task.execute(link); 
      try { 
       document = task.get(); 
      } catch (InterruptedException | ExecutionException e) { 
       e.printStackTrace(); 
      } 
      guests = new ArrayList<>(); 
      Elements elementsUserId = document.select("guest_id"); 
      Elements elementsNumbers = document.select("room"); 
      Elements elementsNames = document.select("name"); 
      ... 


      for (int i = 0; i < elementsNumbers.size(); i++) { 
       GuestBean guest = new GuestBean(); 
       guest.setUserId(elementsUserId.get(i).ownText()); 
       guest.setRoom(elementsNumbers.get(i).ownText()); 
       guest.setName(elementsNames.get(i).ownText()); 
       ... 
       guests.add(guest); 
       count++; 
      } 
      GuestsListAdapter adapter = new GuestsListAdapter(getActivity(), guests); 
      list.setAdapter(adapter); 
     } 
+3

'運行一個過程如在任務開始和形式凍結(約一秒)'由於'文檔= task.get();'。線見[AsyncTask.get()](http://developer.android.com/reference/android/os/AsyncTask.html#get%28long,%20java.util.concurrent.TimeUnit%29)對於更多的細節。 – 2014-12-08 09:40:38

+0

因此,我知道這是掛起。問題是否是對的? task.get()等待AsyncTask完成工作。並等待主線程掛起。我需要在這個時候旋轉我的progressBar.it可以嗎? – 2014-12-08 09:49:33

+0

'我需要在這個時間旋轉我的progressBar'來執行相同的使用'onPostExecute'來執行'doInBackground'結果的選項。參見[安卓的AsyncTask的ListView - JSON(http://www.javacodegeeks.com/2013/06/android-asynctask-listview-json.html)例如,對於更多的幫助 – 2014-12-08 09:55:04

回答

0

AS @ρяσѕρєя Kdocument = task.get();,這裏的線程將得到blocked.You可以通過使用Interfacethis做到了這一點。

0

我發現溶液

public void setData() { 
     new MyAsincTask(getActivity()){ 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
      } 

      @Override 
      protected void onPostExecute(Document document) { 
       super.onPostExecute(document); 
       Elements elementsDate = document.select("date"); 
       Elements elementsName = document.select("name"); 
       Elements elementsSumm = document.select("summa"); 

       for (int i = 0; i < elementsName.size(); i++) { 
        FolioBean folioBean = new FolioBean(); 
        folioBean.setDate(elementsDate.get(i).ownText()); 
        folioBean.setName(elementsName.get(i).ownText()); 
        folioBean.setSumm(elementsSumm.get(i).ownText()); 
        folioBeans.add(folioBean); 
       } 
       TableRow.LayoutParams row_params = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 20f); 
       TableRow.LayoutParams row_params2 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 30f); 

       for (FolioBean folioBean : folioBeans) { 
        TableRow tableRow = new TableRow(getActivity()); 
        TextView data = new TextView(getActivity()); 
        TextView name = new TextView(getActivity()); 
        TextView summ = new TextView(getActivity()); 
        data.setTextSize(10); 
        name.setTextSize(10); 
        summ.setTextSize(10); 
        name.setTextColor(Color.BLACK); 
        summ.setTextColor(Color.BLACK); 
        data.setTextColor(Color.BLACK); 
        data.setText(" " + folioBean.getDate()); 
        name.setText(" " + folioBean.getName() + " "); 
        summ.setText(folioBean.getSumm() + " "); 
        tableRow.addView(data, row_params2); 
        tableRow.addView(name, row_params2); 
        tableRow.addView(summ, row_params); 
        tableFolio.addView(tableRow); 
       } 
      } 
     }.execute(link); 
    }