AsyncTask - 执行后,如何更新视图? [英] AsyncTask - after execution, how to update view?

查看:13
本文介绍了AsyncTask - 执行后,如何更新视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在活动的 onCreate() 事件中,我启动了一个 AsyncTask 以从数据库中检索产品数据.成功完成后,如何更新显示?

In the onCreate() event of an Activity, I have started an AsyncTask to retrieve Product data from a database. After this has been completed successfully, how can I update the display?

元代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.venueviewbasic);
            (..)
    new GetProductDetails().execute();

class GetProductDetails extends AsyncTask<String, String, String> {

    protected String doInBackground(String... params) {

        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                // Check for success tag
                int success;
                try {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("id", vid));
        (.. retrieve and parse data and set new textview contents ..)

文本视图等不会更新.

推荐答案

如果你想在完成过程后从异步更新视图然后你可以使用

If you want to update the view from async after complete process in then you can use

protected void onPostExecute(String result)
    {
        textView.setText(result);
    }

但是如果您想在运行后台进程时更新数据,请使用.例如...

But if you want to update data while running background process then use. For ex...

protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));<------
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {  <-------
         setProgressPercent(progress[0]);
     }

更多详情请参见此链接希望这会帮助你...!

for more detail see this link Hope this will help you...!

这篇关于AsyncTask - 执行后,如何更新视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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