添加进度条到我的应用程序,同时从网站加载数据



我有一个简单的类,像这样扩展活动

public class About extends Activity
{
  private ProgressDialog pdia;
  private TextView tvAbout;
  private WebView vwAbout;
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.about);

    vwAbout = (WebView)findViewById(R.id.vwViewAbout);
    String content = new String();
    try {
        URL url = new URL("http://xxx.prodevAbout.txt");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        while ((str = in.readLine()) != null)
        {
            content+=str + "n";
            vwAbout.loadData(content, "text/html", null);
        }
        in.close();
    } catch (Exception e)
    {
        vwAbout.loadData("error message", "text/html", null);
    }
  }
}

我想做的是在从网站加载内容时添加一个进度条我找到了这个代码

 private ProgressDialog pdia;
 @Override
 protected void onPreExecute(){ 
    super.onPreExecute();
         pdia = new ProgressDialog(yourContext);
         pdia.setMessage("Loading...");
         pdia.show();    
 }
 @Override
 protected void onPostExecute(String result){
    super.onPostExecute(result);
         pdia.dismiss();
 }

并添加了这段代码,但onPreExecute方法无法解决,我该怎么办?

谢谢,

我在我的应用程序中添加进度对话框的方式(直到现在我还没有遇到任何问题)是这样的:

Runnable runable = new Runnable() {
        @Override
        public void run() {
            SofiaLiveCafeActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB){
                        progressDialog = new ProgressDialog(SofiaLiveCafeActivity.this, ProgressDialog.THEME_HOLO_LIGHT);
                    } else {
                        progressDialog = new ProgressDialog(SofiaLiveCafeActivity.this);
                    }
                    progressDialog.setMessage("Loading! Pleasе wait...");
                    progressDialog.setIndeterminate(true);
                    progressDialog.show();
                }
            });

            // CONNECT TO SERVER AND DOWNLOAD DATA

            SofiaLiveCafeActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                    progressDialog.dismiss();
                }
            });
            }
        }
    };
    new Thread(runable).start();

添加了这段代码,但onPreExecute方法无法解析是吗?

onPreExecute没有解决,因为这是AsyncTask的方法,所以需要扩展AsyncTask类来使用onPreExecute,使用AsyncTask更改当前代码:

public class About extends Activity
{
   [...your code here...]
  public void onCreate(Bundle savedInstanceState)
  {
   [...your code here...]
   // execute AsyncTask as 
    new LongOperation(About.this).execute("");
  }
  [...your code here...]
   public class WebapiOperation extends AsyncTask<String, Void, String> {
     Context context;
     WebapiOperation(Context context){
     this.context=context;
    }
      @Override
      protected void onPreExecute() {
         // show ProgressDialog here
         pdia = new ProgressDialog(context);
      }
      @Override
      protected String doInBackground(String... params) {
             // make web service access task here
            return null;
      }      
      @Override
      protected void onPostExecute(String result) {  
            // ProgressDialog here
      }
}

相关内容

  • 没有找到相关文章

最新更新