2014-10-02 58 views
0

我的目標是在沒有互聯網連接的情況下關閉最初的progressdialog(比方說10秒後),然後觸發另一個alertdialog whice提示用戶檢查其互聯網連接並重試。關閉progressdialog並觸發另一個

這裏是我的RemoteDataTask類:

private class RemoteDataTask extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     /* 
     Create the progressdialog 
     */ 
     mProgressDialog = new ProgressDialog(MainActivity.this); 
     //title : 
     mProgressDialog.setTitle("SmartShop. Shopping made easy !"); 
     //message : 
     mProgressDialog.setMessage("Chargement..."); 
     mProgressDialog.setIndeterminate(false); 
     //show the progressdialog...Only if gpslocation is available !! :) 
     if (gps.canGetlocation()){ 
      mProgressDialog.show(); 
      } 
     //mProgressDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     long delayInMillis = 3000; 
     list_of_articles = new ArrayList<Articles>(); 
     try { 
      timer.schedule(new TimerTask() { 
      @Override 
      public void run() { 
       mProgressDialog.dismiss(); 
      } 
     },delayInMillis); 
      // Locate the class table named "Article" in Parse.com 
      ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
        "Article"); 
      // Locate the column named "ranknum" in Parse.com and order list 
      // by ascending 
      //query.orderByAscending("ranknum"); 
      query.whereWithinKilometers("Localisation_Vendeur",device_location,rayon); 
      ob = query.find(); 
      for (ParseObject article : ob) { 
       // Locate images in article_image column 
       ParseFile image = (ParseFile) article.get("Image_Article"); 

       Articles map = new Articles(); 
       map.setArticle_name((String) article.get("Nom_Article")); 
       map.setArticle_vendor((String) article.get("Nom_Vendeur")); 
       //map.setArticle_vendor((String) article.get("reduction")); 
       map.setArticle_image(image.getUrl()); 
       list_of_articles.add(map); 
      } 
     } catch (ParseException e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 

     // Locate the listview in listview_main.xml 
     listview = (ListView) findViewById(R.id.listview); 
     // Pass the results into ListViewAdapter.java 
     adapter = new ListViewAdapter(MainActivity.this, 
       list_of_articles); 
     // Binds the Adapter to the ListView 
     listview.setAdapter(adapter); 
     // Close the progressdialog 
     mProgressDialog.dismiss(); 
    } 



} 

我progressdialog沒有與此代碼解僱。它出什麼問題了 ?我應該在哪裏調用第二個alertdialog「檢查互聯網連接並重試」?

謝謝!

回答

0

您應該只從UI線程執行UI修改。 Timer在其自己的線程中運行其任務,而不是在UI線程中運行。你可以這樣做:

runOnUiThread(new Runnable() { public void run() { 
    mProgressDialog.dismiss(); 
}}); 

並以相同的方式啓動一個新的對話框。

+0

感謝您的回覆,但我不理解那部分「您應該只從UI線程執行UI修改」。你能指點我一個教程/文檔嗎? 順便說一句我把計時器移到了onPreExecute函數中,現在prgressdialog確實關閉了。 – RidRoid 2014-10-03 09:40:23

+0

@RidouaneHicham http://developer.android.com/guide/components/processes-and-threads.html – 2014-10-03 11:06:34

相關問題