2014-08-29 46 views
0

我当前的项目涉及从服务器下载数据 - 这大约需要20秒,所以在此期间我想显示进度条,以便用户知道发生了什么。下载任务在DownloadWebpageTask(AsyncTask)中执行,我在preExecute()中设置进度条的可见性,但没有显示出来。我仔细研究了这里的所有问题,但仍然无法实现。如果我没有在onPostExecute()中隐藏进度条,我可以看到发生了什么 - 即使我在onPreExecute()中设置了progBar.setVisibility(0),直到doInBackground之后,它才会真正显示出来。我试着在onPreExecute中设置一个TextView,看看它是否只是进度条不能正常工作,但这也失败了。在预先执行的AsyncTask中没有显示进度条()

请有人可以帮我在这里,我完全卡住!

这就要求我的AsyncTask:

public void getDTCs(View view) throws URISyntaxException{  

     DtcListSingleton.getInstance().setData(null); 
     status = "DTC Parsing Not Attempted"; 
     noDTCs = 0; 

     TextView listTitle = (TextView) findViewById(R.id.DTCtitle); 
     String newTitle = "Waiting for DTCs"; 
     listTitle.setText(newTitle); 


     String stringURL = "http://192.168.1.129"; 
     //Check connection 
     ConnectivityManager cMgr = (ConnectivityManager)getSystemService(this.CONNECTIVITY_SERVICE); 
     NetworkInfo netInf = cMgr.getActiveNetworkInfo(); 

     // If connected to network carry on with DTC request 
     if(netInf != null && netInf.isConnected()){ 

      //Download the web page as an InputStream 
      new DownloadWebpageTask().execute(stringURL); 

,这是我的AsyncTask:

private class DownloadWebpageTask extends AsyncTask<String,Void,String>{ 
     @Override 
     protected void onPreExecute(){ 
      super.onPreExecute(); 
      progBar = (ProgressBar)findViewById(R.id.loadingDTCs); 
      progBar.setVisibility(0); 
      TextView title = (TextView)findViewById(R.id.DTCtitle); 
      title.setText("Loading DTCs"); 
     } 
     @Override 
     protected String doInBackground(String...urls){ 
      //params[0] is the url 
      try{ 
       return downloadUrl(urls[0]); 
      } 
      catch(IOException e){ 
       return "Unable to retreieve web page. URL may be invalid."; 
      } 

      // DELETE if overcome client.print delay 
      catch (InterruptedException e) { 
       e.printStackTrace(); 
       return "Interrupted Exception from Delaying inputstream"; 
      } 
     } 
     @Override 
     protected void onPostExecute(String result){ 

      AlertDialog.Builder resultURL = new AlertDialog.Builder(ActionReadDTCs.this); 

      resultURL.setTitle("Result of DownloadWebPageTask") 
      .setMessage(result) 

      .setPositiveButton("OK", new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog, int id){ 
        progBar = (ProgressBar)findViewById(R.id.loadingDTCs); 
        progBar.setVisibility(8); 
        return; 
       } 
      }) 

      .show(); 
     } 
    } 
+0

你试过.setVisibility(View.VISIBLE) – PedroHawk 2014-08-29 08:45:20

+0

你的min/max SDK和当前版本的android版本? – 2014-08-29 08:50:41

+0

它改变了文字吗? title.setText(“加载DTC”); ?? – 2014-08-29 08:52:06

回答

0

示例代码看看,如果这个工程?这是否就意味着你正在你的观点呈现出一些错误和隐藏

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <Button 
     android:id="@+id/btnDownload" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/download" 
     android:textSize="14sp" /> 
    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:contentDescription="@string/your_image_will_appear_here" /> 
</LinearLayout> 

而且你的java类

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    /** Reference to the view which should receive the image */ 
    private final WeakReference imageRef;public DownloadImageTask(ImageView imageView) { 
     imageRef = new WeakReference(imageView); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = ProgressDialog.show(DownloadImageActivity.this, "Wait", "Downloading..."); 
    } 

    @Override 
    protected Bitmap doInBackground(String... params) { 
     InputStream input = null; 
     try { 
      URL url = new URL(params[0]); 
      // We open the connection 
      URLConnection conection = url.openConnection(); 
      conection.connect(); 
      input = new BufferedInputStream(url.openStream(), 8192); 
      // we convert the inputStream into bitmap 
      bitmap = BitmapFactory.decodeStream(input); 
      input.close(); 
     } catch (Exception e) { 
      Log.e("Error: ", e.getMessage()); 
     } 
     return bitmap; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(Bitmap bitmap) { 
     progressDialog.dismiss(); 
     if (isCancelled()) { 
      bitmap = null; 
     } 

     if (imageRef != null) { 
      ImageView imageView = imageRef.get(); 
      if (imageView != null &amp;&amp; bitmap != null) { 
       imageView.setImageBitmap(bitmap); 
      } else 
       Toast.makeText(DownloadImageActivity.this, "Error while downloading the image!", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 



<uses-permission android:name="android.permission.INTERNET"/> 

See the result

0

感谢回答,但我已经找到了我的问题。

我还没有发布我的所有代码 - 我实际上是在AsyncTask之后调用另一个函数,并且一旦我将new DownloadWebpageTask.execute(String url)后面的所有代码移动到onPostExecute(),我的进度条显示得很好!