更新的主题UI [英] Update UI from Thread

查看:107
本文介绍了更新的主题UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个线程用于更新一个进度更新我的UI。不幸的是,更新进度条的绘制时,从可运行的进度条消失! 更改progressbars的绘制在的onCreate()的阿瑟赛德作品!

有什么建议?

 公共无效的onCreate(包savedInstanceState){
    RES = getResources();
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.gameone);
    pB.setProgressDrawable(getResources()getDrawable(R.drawable.green)); //**作品**/
    handler.postDelayed(可运行,1);
}

私人可运行可运行=新的Runnable(){
    公共无效的run(){
        runOnUiThread(新的Runnable(){
            公共无效的run()
            {
                // *总进度不出现** /
                pB.setProgressDrawable(getResources()getDrawable(R.drawable.green));
            }
        });
    }
}
 

解决方案

您应该这样做具有的 AsyncTask的(智能底色线程)和<一href="http://developer.android.com/intl/de/reference/android/app/ProgressDialog.html">ProgressDialog

的AsyncTask能够适当且易于使用的用户界面线程。该类允许无需操作线程和/或处理程序进行后台操作并发布在UI线程上的结果。

这是异步任务是由在后台线程运行的计算,其结果发表在UI线程上定义。异步任务由3泛型类型定义,所谓的PARAMS,进展和成效,以及4个步骤,称为开始,doInBackground,processProgress和结束。

4步骤

当执行一个异步任务,任务经过4个步骤:

在preExecute(),执行任务后,立即在UI线程调用。这个步骤是通过示出在用户界面的进度条通常用于设置任务,例如

doInBackground(参数...),在后台线程上preExecute后立即调用()完成执行。这个步骤是用于执行后台计算,可以采取很长的时间。异步任务的参数被传递到这一步。计算的结果,必须通过此步骤被返回,将被传递回的最后一步。这一步也可以使用publishProgress(进展...)公布进展情况的一个或多个单元。这些值公布在UI线程上,在onProgressUpdate(进展...)一步。

onProgressUpdate(进展...),调用publishProgress(进展......)之后的UI线程调用。的执行的计时是不确定的。此方法用于显示的任何形式的用户界面中的进展而背景计算仍在执行。例如,它可以用于动画在文本字段进度条或显示日志

onPostExecute(结果),后台计算完成后在UI线程调用。背景计算的结果被传递给此步骤作为一个参数。 线程规则

有必须遵循此类正常工作的几个线程的规则:

任务实例必须在UI线程上创建。 执行(参数...)必须在UI线程中调用。 不要在preExecute调用(),onPostExecute(结果),doInBackground(参数...),onProgressUpdate(进步...)手动。 该任务只能一次(如果是第二次执行尝试一个异常将被抛出。)

执行

示例code
什么适配器确实在这个例子中并不重要,更重要的是了解你需要使用的AsyncTask来显示该进程的对话框。

 私有类prepareAdapter1延伸的AsyncTask&LT;虚空,虚空,ContactsListCursorAdapter&GT; {
    ProgressDialog对话框;
    @覆盖
    在preExecute保护无效(){
        对话框=新ProgressDialog(viewContacts.this);
        dialog.setMessage(的getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(真正的);
        dialog.setCancelable(假);
        dialog.show();
    }
    / *(非Javadoc中)
     * @see android.os.AsyncTask#doInBackground(PARAMS [])
     * /
    @覆盖
    保护ContactsListCursorAdapter doInBackground(空... PARAMS){
        CUR1 = objItem.getContacts();
        startManagingCursor(CUR1);

        适配器1 =新ContactsListCursorAdapter(viewContacts.this,
                R.layout.contact_for_listitem,CUR1,新的String [] {},新的INT [] {});

        返回适配器1;
    }

    保护无效onPostExecute(ContactsListCursorAdapter结果){
        list.setAdapter(结果);
        dialog.dismiss();
    }
}
 

I want to update my UI from a Thread which updates a Progressbar. Unfortunately, when updating the progressbar's drawable from the "runnable" the progressbar disappears! Changing the progressbars's drawable in onCreate() on the otherside works!

Any Suggestions?

public void onCreate(Bundle savedInstanceState) {
    res = getResources();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gameone);
    pB.setProgressDrawable(getResources().getDrawable(R.drawable.green)); //**Works**/
    handler.postDelayed(runnable, 1);       
}

private Runnable runnable = new Runnable() {
    public void run() {  
        runOnUiThread(new Runnable() { 
            public void run() 
            { 
                //* The Complete ProgressBar does not appear**/                         
                pB.setProgressDrawable(getResources().getDrawable(R.drawable.green)); 
            } 
        }); 
    }
}

解决方案

You should do this with the help of AsyncTask (an intelligent backround thread) and ProgressDialog

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called begin, doInBackground, processProgress and end.

The 4 steps

When an asynchronous task is executed, the task goes through 4 steps:

onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter. Threading rules

There are a few threading rules that must be followed for this class to work properly:

The task instance must be created on the UI thread. execute(Params...) must be invoked on the UI thread. Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually. The task can be executed only once (an exception will be thrown if a second execution is attempted.)

Example code
What the adapter does in this example is not important, more important to understand that you need to use AsyncTask to display a dialog for the progress.

private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > {
    ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(viewContacts.this);
        dialog.setMessage(getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }
    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected ContactsListCursorAdapter doInBackground(Void... params) {
        cur1 = objItem.getContacts();
        startManagingCursor(cur1);

        adapter1 = new ContactsListCursorAdapter (viewContacts.this,
                R.layout.contact_for_listitem, cur1, new String[] {}, new int[] {});

        return adapter1;
    }

    protected void onPostExecute(ContactsListCursorAdapter result) {
        list.setAdapter(result);
        dialog.dismiss();
    }
}

这篇关于更新的主题UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆