0

我有一个ListViewActivity1. OnItemClick,我正在导航到另一个 Activity2我有一些数据库代码的活动。

现在的问题是从 DB 获取数据需要一些时间,因此在从 导航 Activity1到 时 Activity2,会出现几秒钟的空白屏幕。

为了克服这个问题,我尝试了ProgressDialogProgressDialog根本没有出现。

活动 2 中的代码片段:

public class Activity2 extends Activity implements Runnable {
@Override
public void onCreate(Bundle savedInstanceState) {
Thread thread = new Thread();
        thread.start();
        run();
}

@Override
    public void run() {
        plansDesc = dbMgr.chkRecord(
                "SELECT Description FROM SubPlan where Title='" + subTitle
                        + "'", "Description");

        ProgressDialog progDailog = ProgressDialog.show(
                SubPlanDescActivity.this, "Plan Information", "Loading...",
                true);
        try {
            if (plansDesc != "") {
                tvDesc.setText(Html.fromHtml(plansDesc));
            } else {
                tvDesc.setText(Html
                        .fromHtml("<b>No Information Available...</b>"));
            }
        } catch (Exception e) {
        }
        handler.sendEmptyMessage(0);
        progDailog.dismiss();
    }

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
    }
};

任何帮助表示赞赏。

4

7 回答 7

4

尝试 AsyncTask 而不是线程

请参阅下面的代码

@Override
public void onCreate(Bundle savedInstanceState) {
   new ProgressTask().execute();
}

现在创建类

private class ProgressTask extends AsyncTask<String, Void, Boolean> {

  private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);

  protected void onPreExecute() {
    this.dialog.setMessage("Please wait");
    this.dialog.show();
  }

  protected Boolean doInBackground(final String... args) {
    try {

        plansDesc = dbMgr.chkRecord(
            "SELECT Description FROM SubPlan where Title='" + subTitle
                    + "'", "Description");
        return true;
    } catch (Exception e) {
        Log.e("tag", "error", e);
        return false;
    }
  }

  @Override
  protected void onPostExecute(final Boolean success) {

    if (dialog.isShowing()) {
        dialog.dismiss();
    }

    if (success) {
        // display UI
        if (plansDesc != "") {
            tvDesc.setText(Html.fromHtml(plansDesc));
        } else {
            tvDesc.setText(Html
                    .fromHtml("<b>No Information Available...</b>"));
        }
    }
  }
}
于 2012-07-16T08:55:38.003 回答
1

您必须关闭处理程序中的进度对话框。

   private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        progDailog.dismiss();
        }
    };

尝试像这样修改代码,

@Override
public void run() {
     handler.sendEmptyMessage(1);
    plansDesc = dbMgr.chkRecord(
            "SELECT Description FROM SubPlan where Title='" + subTitle
                    + "'", "Description");


    try {
        if (plansDesc != "") {
            tvDesc.setText(Html.fromHtml(plansDesc));
        } else {
            tvDesc.setText(Html
                    .fromHtml("<b>No Information Available...</b>"));
        }
    } catch (Exception e) {
    }
    handler.sendEmptyMessage(0);

}

而你的处理程序是这样的,

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      if(msg.what==0)
            {
             progDailog.dismiss();
               }
      if(msg.what==1)
      {
          ProgressDialog progDailog = ProgressDialog.show(
            SubPlanDescActivity.this, "Plan Information", "Loading...",
            true);
         }

    }
};
于 2012-07-16T08:39:45.013 回答
1

写这个:

public class Activity2 extends Activity implements Runnable {
Thread thread;
ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yourLayout);


dialog=ProgressDialog.show(this, "","Loading...",true);
dialog.show();
thread = new Thread(this);
thread.start();

}

public void run() {
        plansDesc = dbMgr.chkRecord(
            "SELECT Description FROM SubPlan where Title='" + subTitle
                    + "'", "Description");


    try {
        if (plansDesc != "") {
            tvDesc.setText(Html.fromHtml(plansDesc));
        } else {
            tvDesc.setText(Html
                    .fromHtml("<b>No Information Available...</b>"));
        }
    } catch (Exception e) {
    }

        handler.sendEmptyMessage(0);        
    }



 private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
              if(dialog!=null && dialog.isShowing()){
                 dialog.dismiss();
                 }
        }
   };
于 2012-07-16T08:43:04.597 回答
0

如果你想加载数据,那么使用AsyncTask是最好的解决方案。

于 2012-07-16T08:49:08.617 回答
0

在您的 onCreate 方法中,您必须将“Runnable”元素(此处为“this”)作为线程对象参数。然后,您不需要调用“运行”方法,它会使用 Thread 对象的 start() 方法自动调用。

代码示例:

Thread thread = new Thread(this);
thread.start();

我不确定,这完全解决了你的问题,但这是使用带有可运行接口的线程的好方法。

于 2012-07-16T08:52:36.200 回答
0
    new AsyncTask<Void, Void, Cursor>() {

        protected void onPreExecute() {
            showProgress...
        }

        protected Cursor doInBackground(Void... structs) {
            Loading data...

            return result;
        }

        protected void onPostExecute(Cursor result) {
            Hide progress...

        }

    }.execute();
于 2012-07-16T08:55:05.373 回答
0

在 Activity 中显示进度对话框,而不是在数据库线程中。完成后使用 Handler 从线程发送消息并隐藏对话框。

于 2012-07-16T09:04:53.427 回答