在构建大ui时显示进度对话框

时间:2012-09-24 17:29:30

标签: android multithreading android-asynctask progress-bar

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

代码是这样的:

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,我总是异常崩溃。

任何想法?

2 个答案:

答案 0 :(得分:0)

答案是你不能!要查看它必须在UI线程上运行的ProgressDialog的进度。并且创建UI已经在UI线程上运行,这就是您的进度对话框显示无响应的原因。此外,您不能使用AsyncTask创建UI,您将始终获得“从错误的线程调用”异常。这意味着您必须在UI线程上构建UI。

我正常做的是在加载UI时有一个启动画面,当其他所有内容准备就绪时我将其删除

答案 1 :(得分:0)

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

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