2013-10-18 66 views
0

我正在關注this tutorial 以瞭解如何製作進度條。我試圖在我的活動之上顯示進度條,並讓它在後臺更新活動的表格視圖。從異步任務更新表android

所以我創建了對話的異步任務,需要一個回調:

package com.lib.bookworm; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.os.AsyncTask; 



public class UIThreadProgress extends AsyncTask<Void, Void, Void> { 
    private UIThreadCallback callback = null; 
    private ProgressDialog dialog = null; 
    private int maxValue = 100, incAmount = 1; 
    private Context context = null; 

    public UIThreadProgress(Context context, UIThreadCallback callback) { 
     this.context = context; 
     this.callback = callback; 
    } 

    @Override 
    protected Void doInBackground(Void... args) { 
     while(this.callback.condition()) { 
      this.callback.run(); 
      this.publishProgress(); 
     } 
     return null; 
    } 

    @Override protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
     dialog.incrementProgressBy(incAmount); 
    }; 

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

     dialog = new ProgressDialog(context); 
     dialog.setCancelable(true); 
     dialog.setMessage("Loading..."); 
     dialog.setProgress(0); 
     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     dialog.setMax(maxValue); 
     dialog.show(); 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 

     if (this.dialog.isShowing()) { 
      this.dialog.dismiss(); 
     } 
     this.callback.onThreadFinish(); 
    } 

} 

在我的活動:

final String page = htmlPage.substring(start, end).trim(); 

//Create new instance of the AsyncTask.. 

new UIThreadProgress(this, new UIThreadCallback() { 

    @Override 
    public void run() { 
     row_id = makeTableRow(row_id, layout, params, matcher); //ADD a row to the table layout. 
    } 

    @Override 
    public void onThreadFinish() { 
     System.out.println("FINISHED!!");      
    } 

    @Override 
    public boolean condition() { 
     return matcher.find(); 
    } 
}).execute(); 

所以上面創建一個異步任務運行更新表格佈局活動,同時顯示進度條,顯示已完成多少工作..

但是,我收到一條錯誤消息,說只有啓動活動的線程才能更新其視圖。我試圖改變我的異步任務的運行於以下內容:

MainActivity.this.runOnUiThread(new Runnable() { 
    @Override public void run() { 
     row_id = makeTableRow(row_id, layout, params, matcher); //ADD a row to the table layout. 
    } 
} 

但是這給了我同步錯誤。任何想法如何,我可以顯示進度,並在同一時間更新我的表中的背景是什麼?

目前我的UI看起來像:

enter image description here

回答

1

無論您在UI中進行的更新是否正在進行更新,請使用Global Variables來傳遞值或使用Getter Setter

這是一個簡單的例子,來自我目前的一個項目。 它改變了LinearLayout的寬度,LinearLayout用作進度條,還用X%更新文本視圖。上午致電onProgressUpdate

public class Updater extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     width = getWindowManager().getDefaultDisplay().getWidth(); 
     Log.wtf(tag, "width" + width); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     while (updated < sleep) { 
      try { 
       Thread.sleep(updateEveryXmillSec); 
       updated = updated + updateEveryXmillSec; 
       publishProgress(); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
     mTextView.setText((int) (100 * updated/sleep) + " %"); 
     xwidth = (width * ((int) (100 * updated/sleep))/100); 
     mLayout.setLayoutParams(new RelativeLayout.LayoutParams(xwidth, 
       height)); 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 

     startActivity(new Intent(getApplicationContext(), Main.class)); 
     finish(); 

    } 
} 

呼叫new Updater().execute();更新到觸發動作。

0

你應該從行數據分割你的UI數據。使其中包含表中的數據顯示RowObject:

class RowData { 
    String program; 
    String course; 
    String bookName; 

    // get/set etc 
} 

您可以填寫UIThreadProgress類的run方法該對象,並把它推到一個同步列表。

在onProcessUpdate()中,您可以根據同步列表構建View對象並將其添加到View Hierachie。你現在在UI線程上,並且應該可以添加。

在此期間,您必須關心同步列表。因爲Background Thread和UI Thread將同時添加和刪除對象。同步將在這裏幫助。根據算法計算所需數據的速度,比同步列表更快的方法更好。但是這個想法總是一樣的。您必須拆分數據和查看操作。