2013-10-18 140 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将同时添加和删除对象。同步将在这里帮助。根据算法计算所需数据的速度,比同步列表更快的方法更好。但是这个想法总是一样的。您必须拆分数据和查看操作。