2013-02-26 54 views
0

在我的android應用程序中,我將一些數據從服務器數據庫複製到本地數據庫。該代碼發佈如下。這可能需要15分鐘以上。對於較慢的連接,可能會超出。所以我想顯示一個等待進度條,應該顯示「請稍等」,直到任務完成。我提到很多東西,但是我無法理解如何在下面的代碼中使用等待進度條。在android中等待進度條

refresh.setOnClickListener(new OnClickListener(){ 

public void onClick(View v) { 
    mySQLiteAdapter.openToWrite(); 
    mySQLiteAdapter.deleteAll(); 
    radapter.openToWrite(); 
    radapter.deleteAll(); 
    uadapter.openToWrite(); 
    uadapter.deleteAll(); 
      try 
       { 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost("http://xxxxxxxxxxxxxxxxxxxxx"); 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 
         is = entity.getContent(); 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       try { 
        BufferedReader reader = new BufferedReader(new InputStreamReader(
          is, "iso-8859-1"), 8); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
        is.close(); 
        json = sb.toString(); 
       } catch (Exception e) { 
       Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show(); 
       } 

       try { 
        jObj = new JSONObject(json); 
        contacts = jObj.getJSONArray("get"); 
        for(int i = 0; i < contacts.length(); i++){ 
        JSONObject c = contacts.getJSONObject(i); 
         if(!c.isNull("rname"))// || c.getString("rcode").equals(null)) 
         { 
          rname = c.getString("rname"); 
          rcode = c.getString("rcode"); 
          radapter.insert(rcode, rname); 
         } 
         else 
         { 
          rname = ""; 
          rcode = c.getString("rcode"); 
          radapter.insert(rcode, rname); 
         } 
        } 
        Toast.makeText(getBaseContext(), " Ryot BackUp completed", Toast.LENGTH_SHORT).show(); 

       } catch (JSONException e) { 
        Toast.makeText(getBaseContext(), "Error"+e.toString(), Toast.LENGTH_LONG).show(); 
       } 
       adapter.deleteAll(); 
       oadapter.deleteAll(); 
       e2.setText("Back Up completed"); 
      } 

     }); 

有人幫助我。在此先感謝

+0

瞭解水平進度條和線程以相應處理它。 – 2013-02-26 07:02:57

回答

5

使用Progress Dialog顯示文本「請稍候...」和下面thread實現代碼:

 final ProgressDialog progressDialog = ProgressDialog.show(this, "", "Please wait..."); 
     new Thread() { 
      public void run() { 
       try{ 
         //your code here..... 
        } 
       catch (Exception e) { 
        Log.e("tag", e.getMessage()); 
        } 
       // dismiss the progress dialog 
         progressDialog.dismiss(); 
        } 
       }.start(); 
+0

是啊!我需要。謝謝。我現在檢查 – 2013-02-26 07:05:57

+0

將progressDialog放在OnClickListener的外面 – 2013-02-26 07:15:40

+0

我試過我的活動name.this而不是這個。現在我解決了錯誤並嘗試運行我的應用程序 – 2013-02-26 07:16:53

1

嘗試使用的AsyncTask,並使用更新onProgressUpdate方法