【问题标题】:Android Show a dialog until a thread is done and then continue with the programAndroid 显示一个对话框,直到一个线程完成,然后继续程序
【发布时间】:2010-08-07 15:38:13
【问题描述】:

我正在从我的 Android 应用程序的数据库中读取几千个项目,并希望显示一个加载对话框,启动一个线程并读取数据库,然后关闭对话框,然后用数据填充列表视图。 我似乎无法弄清楚这一点。

我弹出并消失了对话框,但我不知道在线程完成后如何填充列表视图。

有什么想法吗?

【问题讨论】:

    标签: android multithreading listview


    【解决方案1】:

    使用 AsyncTask,在 onPostExecute() 中填充列表视图。

    http://www.screaming-penguin.com/node/7746

    private class InsertDataTask extends AsyncTask<Void, Void, Void> {
    
      private final ProgressDialog dialog = new ProgressDialog(Main.this);
    
      // can use UI thread here
      protected void onPreExecute() {
         this.dialog.setMessage("Inserting data...");
         this.dialog.show();
      }
      // automatically done on worker thread (separate from UI thread)
      protected Void doInBackground(final String... args) {
         //do something in background, i.e. loading data
         return null;
      }
    
      // can use UI thread here
      protected void onPostExecute(final Void unused) {
         if (this.dialog.isShowing()) {
            this.dialog.dismiss();
         }
         // populate list here
      }
    }
    

    ...

    new InsertDataTask ().execute();
    

    也很有帮助: http://developer.android.com/resources/articles/painless-threading.html

    【讨论】:

    • 我在此处使用您的答案来搜索位置,但“ProgressDialog”出现的时间非常短暂,并立即消失(几乎像闪光灯一样)。我希望它一直显示,直到找到位置。请告诉我如何做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多