当表填充大量数据时,ProgressDialog冻结

时间:2013-07-10 18:41:06

标签: android multithreading progressdialog

你永远不会使用UI线程(主线程)之外的UI东西。但在这种情况下该怎么做:

运行新线程以检索大量数据。同时,显示一个指示工作的ProgressDialog。检索数据时,调用主线程并填充表。当使用大量数据填充表时,ProgressDialog被“冻结”几秒钟(因为UI线程忙于填充表格)。

有没有办法让ProgressDialog在表格填充时不“冻结”?

public class MyActivity extends Activity  {
    private ProgressDialog progressDialog;
    private TableLayout tableLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tableLayout = (TableLayout)v.findViewById(R.id.myTable);

        progressDialog = ProgressDialog.show(this, "", "Loading...");
        new Thread(new Runnable() {
            @Override
            public void run() {
                final List<Data> data = fetchBigDataFromInternet();
                MyActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tableLayout.removeAllViews();
                        for(Data d : data) { // This makes the progressDialog to freeze
                            TableRow tableRow = new TableRow(MyActivity.this);
                            tableLayout.addView(tableRow);
                         }
                    progressDialog.dismiss();
               ....
    }
}

1 个答案:

答案 0 :(得分:2)

是的,有一种方法可以让对话框不要冻结,那就是使用“延迟加载”技术,如果要填充的数据量巨大,则将其分成较小的部分,并相应地填充,您需要实现一种机制来了解何时需要填充新的数据“片段”,这在具有大量数据的应用程序中非常常见,通常使用“更新”或“刷新”消息... < / p>

希望这会有所帮助..

问候!