从异步任务android更新表

时间:2013-10-18 21:49:53

标签: android multithreading asynchronous

我正在关注this tutorial 学习如何进步吧。我正在尝试在我的活动之上显示进度条,并让它在后台更新活动的表格视图。

所以我为进行回调的对话框创建了一个异步任务:

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();

因此,上面创建了一个异步任务来运行以更新表格布局活动,同时显示进度条,显示已完成的工作量。

但是,我收到一条错误消息,说只有启动该活动的线程才能更新其视图。我尝试将我的Async Task的运行更改为以下内容:

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

但是这给了我同步错误..任何想法如何显示进度并同时在后台更新我的表?

目前我的用户界面如下:

enter image description here

2 个答案:

答案 0 :(得分:1)

无论您在UI中进行的任何更新都在进行更新,请使用Global Variables传递值或使用Getter Setter

这是一个简单的例子,来自我当前的一个项目。 它会更改LinearLayout的宽度,该宽度充当进度条,并使用X%更新textview。我是通过致电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();以触发操作。

答案 1 :(得分:0)

您应该从行数据中分割UI数据。创建一个RowObject,其中包含要在表中显示的数据:

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

    // get/set etc
}

您可以在UIThreadProgress类运行方法中填充此对象,并将其推送到同步列表。

在onProcessUpdate()中,您可以根据同步列表构建视图对象,并将其添加到View Hierachie。你现在在UI线程上,应该可以添加。

在此过程中你必须关心同步列表。因为后台线程和UI线程会同时添加和删除对象。同步将有助于此。根据算法的速度来计算所需的数据,比同步列表更快的方法更好。但是这个想法总是一样的。您必须拆分数据和查看操作。