0

在我的活动中,我从网络上获取了一些大数据,并且在获取这些数据时,我想向用户展示一个带有旋转轮的 ProgressDialog。我只能将此代码放入线程中,对吗?问题是,在我得到这些数据后,我需要将它作为 TableRows 插入到我的 tableLayout 中,并且似乎无法从线程访问 TableLayout

我该怎么做才能显示此进度对话框并能够从线程访问表格布局?线程结束时是否发生了任何事件?

我的代码失败:

_tableLayout.addView(_tableRowVar, new TableLayout.LayoutParams(
      LayoutParams.FILL_PARENT,
      LayoutParams.FILL_PARENT));

我的完整代码是:

final ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
            "Getting data.\nPlease wait...",true);

new Thread() 
{
    public void run() 
    {
        try
        {
            TableLayout _tableLayout;
            _tableLayout = (TableLayout)MyActivity.this.findViewById(R.id.tableLayoutID);

            List<String> data = getDataFromWeb();

            // Get the data and bind it into the table
            publishTableLayoutWithTableRows(_tableLayout, data );

        }
        catch (Exception e) {
            new AlertDialog.Builder(MyActivity.this)
                 .setMessage(e.getMessage())
             .show();
        }
        dialog.dismiss();
    }
}.start();
4

1 回答 1

0

你应该使用AsyncTask

您还会注意到can use UI thread here可以使用 UI 的方法旁边的注释。

于 2010-06-05T16:56:46.093 回答