【问题标题】:Show progress dialog while build big ui在构建大 ui 时显示进度对话框
【发布时间】:2012-09-24 17:29:30
【问题描述】:

我知道这是一个常见的问题。我多次阅读这个网站和许多教程,但没有机会工作。

代码是这样的:

public class mostraRisultatiActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mostra_risultati);

    int cnt = 0;
    final TableLayout tl = (TableLayout) findViewById(R.id.tabellaRisultati);

    for (Esame e : Db.risposteDate){
        TableRow tr = new TableRow(this);

        TextView lbl1 = new TextView(this);
        TextView lbl2 = new TextView(this);
        TextView lbl3 = new TextView(this);
        tr.setGravity(Gravity.CENTER);
        lbl1.setGravity(Gravity.CENTER_VERTICAL);
        lbl3.setGravity(Gravity.CENTER);

        lbl1.setTextColor(android.graphics.Color.RED);
        lbl2.setTextColor(android.graphics.Color.RED);
        lbl3.setTextColor(android.graphics.Color.RED);

        lbl1.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1 ) );
        lbl2.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 7 ) );
        lbl3.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1 ) );

        lbl1.setText(e.getDomandaNum()+"");
        lbl2.setText(Html.fromHtml(e.getTesto()+"<br />"));
        lbl3.setText(e.getRispostaData());

        tr.addView(lbl1);
        tr.addView(lbl2);
        tr.addView(lbl3);
        tl.addView(tr);
        if (e.getRispostaData() != null && e.getRispostaData().equals(e.getRispostaReale())){
            cnt++;
            lbl1.setTextColor(android.graphics.Color.GREEN);
            lbl2.setTextColor(android.graphics.Color.GREEN);
            lbl3.setTextColor(android.graphics.Color.GREEN);
        }
    }
    double percentualeSucceso = (cnt*100)/Db.risposteDate.size();

    final TextView risposteTotTV = (TextView) findViewById(R.id.risultatiTot);
    final TextView riassuntorisTV = (TextView) findViewById(R.id.risRiassunto);
    final TextView esitoTV = (TextView) findViewById(R.id.esito);

    risposteTotTV.setText(Html.fromHtml("<b><big>TOT</big></b>" +  "<br />" + 
            Db.risposteDate.size()));
    riassuntorisTV.setText(Html.fromHtml("<b>Risposte corrette: </b>" +cnt+  "<br />" + 
            "Percentuale: "+percentualeSucceso+"%"));
    if (percentualeSucceso >= 90) {
        esitoTV.setText(Html.fromHtml("<b><big>Esito</big></b>  <br />" + 
                                      "<small>PROMOSSO</small>"));
    } else {
        esitoTV.setText(Html.fromHtml("<b><big>Esito</big></b>  <br />" + 
                                      "<small>BOCCIATO</small>"));
    }

}

}

加载 ui 时我想要一个进度条(尤其是 for 循环)

我尝试了一个线程:所有工作,但在加载 ui 之前没有进度显示(没用) 我尝试使用 asyncTask 并且总是出现异常崩溃。

有什么想法吗?

【问题讨论】:

  • 尝试 AsyncTask 时遇到什么异常?
  • 现在不记得了,我因为沮丧而删除了所有内容。但有很多事情,第一个是 do_background() 中的错误(我所有的代码都在那里)
  • 与其尝试在 onCreate 中构建一个包含大量行和视图的可能很长的表,不如使用 ListView 并按需绘制 [可见] 行?您应该会发现这执行得很快,并且不需要 ProgressDialog
  • 你说ListView的性能比TableLayout好?我从未使用过 ListView,但我试了一下
  • ListView 如果您的表格的行数多于可见行数(不滚动),那么绝对是要走的路。需要进行一些研究才能了解它们的工作原理,但非常值得。

标签: android multithreading android-asynctask progress-bar


【解决方案1】:

嗯,我只用一个非常基本的Button 测试了这个解决方案,但它确实有效,所以这里是:

您可以使用AsyncTask 创建和设置Views。我尝试使用按钮,任何修改/创建View 的调用都可以在 AsyncTask 中正常工作,直到您将视图添加到布局中。添加后,任何修改必须在 UI 线程上完成。因此,您的所有设置都将在doInBackground() 中进行,所有addView() 调用都将在onPostExecute() 中进行。在onPostExecute() 之后,必须在 AsyncTask 中对 View 进行更多更改。

【讨论】:

  • tr.addView(lbl1); tr.addView(lbl2); tr.addView(lbl3); tl.addView(tr);这些继续 onPostExecute?
    还是只有 tl.addView(tr);?
【解决方案2】:

答案是你不能!要查看 ProgressDialog 的进度,它必须在 UI 线程上运行。并且创建您的 UI 已经在 UI 线程上运行,这就是您的进度对话框出现无响应的原因。你也不能使用 AsyncTask 来创建 UI,你总是会得到“从错误的线程调用”异常。这意味着您必须在 UI 线程上构建您的 UI。

我通常会在加载 UI 时显示启动画面,并在其他一切准备就绪时将其移除

【讨论】:

  • 所以?这样做的最佳策略是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多