如何添加“正在加载......”对话框到Android应用程序

时间:2012-05-05 08:04:34

标签: android dialog loading

我有一个简单的Android应用程序来访问Web服务。我希望显示一个对话框"Loading . . .",直到加载结果并在加载结果后将其解除。我已经使用了这段代码但是没有显示加载消息:

public class LoadingActivity extends Activity {
   private static final String SOAP_ACTION = "http://ws.eretailer.com/";
   private static final String METHOD_NAME = "dataSender";
   private static final String NAMESPACE = "http://ws.eretailer.com/dataSender/";
   private static final String URL = "http://175.157.128.207:8085/Eretailer/services/EretailerService?wsdl";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final ProgressDialog pd = new ProgressDialog(this);
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pd.setMessage("Loading. . .");
    pd.show();
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);          

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);
    pd.show();
    HttpTransportSE ht = new HttpTransportSE(URL);
    try {

        ht.call(SOAP_ACTION, envelope);
        SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        SoapPrimitive s = response;

        pd.dismiss();
           String str = s.toString();
           String arr1[] = str.split(" ");

           TextView tv = new TextView(this);

           for(int i = 0; i<arr1.length;i++){
           tv.append("order ID :"+arr1[i]+"\n");
          }
           setContentView(tv);

       } catch (Exception e) {

           e.printStackTrace();
       }
}
}

如何更正此问题?

3 个答案:

答案 0 :(得分:2)

您可以尝试以下代码

 private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected void onPreExecute() {
        pd.show();
        super.onPreExecute();
    }
    @Override
    protected String doInBackground(String... urls) {
        String response = "";
         HttpTransportSE ht = new HttpTransportSE(URL);
            try {

                ht.call(SOAP_ACTION, envelope);
                SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                SoapPrimitive s = response;

                pd.dismiss();
                   String str = s.toString();
                   String arr1[] = str.split(" ");

                   TextView tv = new TextView(this);

                   for(int i = 0; i<arr1.length;i++){
                   tv.append("order ID :"+arr1[i]+"\n");
                  }
                   setContentView(tv);

               } catch (Exception e) {

                   e.printStackTrace();
               }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        pd.dismiss();
    }
}

资源:http://www.vogella.com/articles/AndroidPerformance/article.html

答案 1 :(得分:1)

最好将Android-AsyncTask用于您的Web请求。然后在ProgressDialog的AsyncTask中启动onPreExecute()并在onPostExecute()中关闭它(在此处更新您的UI以获取Web响应的结果)。还将Web-Request代码放在AsyncTask的doInBackGround()中。

例如,请查看Android AsyncTask Example

这绝不会阻止主要UI线程。用户体验的性能更佳。

答案 2 :(得分:0)

首先评论pd.dismiss()并检查其加载是否到来,然后在下载完成后使用pd.dismiss()。