【发布时间】: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