2016-09-26 177 views
0

我有一個應用程序,其中onCreate其發送請求到服務器,我又添加了一個SwipeRefresh,當我再次向下滑動時,請求被髮送到服務器。如何在刷新時刷新進度對話框在Android中刷新?

問題是,當我向下滑動時,ProgressDialog顯示給我,我不想要。我想要的是,如果刷卡刷新,然後不顯示ProgressDialog,否則顯示ProgressDialog

代碼: -

m_SwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
    @Override 
    public void onRefresh() { 
     getWalletBalance(); 
    } 
}); 

/* Here in this method we send request to server for wallet balance */ 
private void getWalletBalance() { 
    CLoginSessionManagement s_oSessionManagement = new CLoginSessionManagement(getApplicationContext());// making object of Registartion session management 
    // retreive user data from shared preferencce........ 
    HashMap<String, String> user = s_oSessionManagement.getLoginDetails();// getting String from Regisatrtion session 
    String m_szMobileNumber = user.get(CLoginSessionManagement.s_szKEY_MOBILE).trim(); 
    String m_szEncryptedPassword = user.get(CLoginSessionManagement.s_szKEY_PASSWORD).trim(); 
    try { 
     String json; 
     // 3. build jsonObject 
     JSONObject jsonObject = new JSONObject(); 
     jsonObject.put("agentCode", m_szMobileNumber);// sending mobile no.(static right know becuse of ser side data on other is null 
     jsonObject.put("pin", m_szEncryptedPassword);// same here as said above 
     // 4. convert JSONObject to JSON to String 
     json = jsonObject.toString(); 
     Log.i(TAG, "Server request:-" + json); 
     //here I am getting error 
     m_Dialog = DialogUtils.showProgressDialog(getApplicationContext(),"Fetching wallet details..."); 
     final String s_szWalletURL = "http://metallica/getWalletBalanceInJSON"; 
     RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
     JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, s_szWalletURL, jsonObject, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       Log.e(TAG, "Server Response:-" + response); 

       if (m_SwipeRefresh.isRefreshing()){ 
        m_SwipeRefresh.setRefreshing(false); 
       }else { 
        m_Dialog.dismiss(); 
       } 
       try { 
        int nResultCodeFromServer = Integer.parseInt(response.getString("resultcode")); 

        if (nResultCodeFromServer == CStaticVar.m_kTRANSACTION_SUCCESS) { 
         s_szWalletBalance = response.getString("walletbalance").trim();// get wallet balance fro response 
         String trimwalletBalance = s_szWalletBalance.substring(0, s_szWalletBalance.indexOf("."));// trim waalet balance from response. 
         CWalletDataModel.getInstance().setS_szWalletBalance(trimwalletBalance);// set wallet balance 
         // showing wallet transaction in textView.... 
         m_WalletText.setText(CWalletDataModel.getInstance().getS_szWalletBalance()); 
        } else if (nResultCodeFromServer == CStaticVar.m_kCONNECTION_NOT_AVAILABLE) { 
         CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Connection not available", getApplicationContext()); 
        } else if (nResultCodeFromServer == CStaticVar.m_kTIMED_OUT) { 
         CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Timed Out", getApplicationContext()); 
        } else if (nResultCodeFromServer == CStaticVar.m_kTECHNICAL_FAILURE) { 
         CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Technical Failure", getApplicationContext()); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e(TAG, "Server Error:-" + error); 
       m_Dialog.dismiss(); 
       if (m_SwipeRefresh.isRefreshing()){ 
        m_SwipeRefresh.setRefreshing(false); 
       }else { 
        m_Dialog.dismiss(); 
       } 
       if (error instanceof TimeoutError) { 
        CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Connection time out! please try again", getApplicationContext()); 
       } else if (error instanceof NetworkError) { 
        CSnackBar.getInstance().showSnackBarError(m_MainLayout, "No Internet connection", getApplicationContext()); 
       } 
      } 
     }); 

     requestQueue.add(jsonObjectRequest); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

回答

0
 Define this variable in your activity 
     boolean isShowProgressDialog=true; 



     m_SwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
       @Override 
       public void onRefresh() { 
        isShowProgressDialog=false; 
        getWalletBalance(); 
       } 
      }); 


    private void getWalletBalance() { 
     CLoginSessionManagement s_oSessionManagement = new CLoginSessionManagement(getApplicationContext());// making object of Registartion session management 
     // retreive user data from shared preferencce........ 
     HashMap<String, String> user = s_oSessionManagement.getLoginDetails();// getting String from Regisatrtion session 
     String m_szMobileNumber = user.get(CLoginSessionManagement.s_szKEY_MOBILE).trim(); 
     String m_szEncryptedPassword = user.get(CLoginSessionManagement.s_szKEY_PASSWORD).trim(); 
     try { 
      String json; 
      // 3. build jsonObject 
      JSONObject jsonObject = new JSONObject(); 
      jsonObject.put("agentCode", m_szMobileNumber);// sending mobile no.(static right know becuse of ser side data on other is null 
      jsonObject.put("pin", m_szEncryptedPassword);// same here as said above 
      // 4. convert JSONObject to JSON to String 
      json = jsonObject.toString(); 
      Log.i(TAG, "Server request:-" + json); 



if(isShowProgressDialog){ 

      isShowProgressDialog=true; 
      m_Dialog = DialogUtils.showProgressDialog(getApplicationContext(),"Fetching wallet details..."); 

} 
      final String s_szWalletURL = "http://metallica/getWalletBalanceInJSON"; 
      RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
      JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, s_szWalletURL, jsonObject, new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.e(TAG, "Server Response:-" + response); 

        if (m_SwipeRefresh.isRefreshing()){ 
         m_SwipeRefresh.setRefreshing(false); 
        }else { 

         if(m_Dialog!=null && m_Dialog.isShowing()){ 
         m_Dialog.dismiss();} 
        } 
        try { 
         int nResultCodeFromServer = Integer.parseInt(response.getString("resultcode")); 

         if (nResultCodeFromServer == CStaticVar.m_kTRANSACTION_SUCCESS) { 
          s_szWalletBalance = response.getString("walletbalance").trim();// get wallet balance fro response 
          String trimwalletBalance = s_szWalletBalance.substring(0, s_szWalletBalance.indexOf("."));// trim waalet balance from response. 
          CWalletDataModel.getInstance().setS_szWalletBalance(trimwalletBalance);// set wallet balance 
          // showing wallet transaction in textView.... 
          m_WalletText.setText(CWalletDataModel.getInstance().getS_szWalletBalance()); 
         } else if (nResultCodeFromServer == CStaticVar.m_kCONNECTION_NOT_AVAILABLE) { 
          CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Connection not available", getApplicationContext()); 
         } else if (nResultCodeFromServer == CStaticVar.m_kTIMED_OUT) { 
          CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Timed Out", getApplicationContext()); 
         } else if (nResultCodeFromServer == CStaticVar.m_kTECHNICAL_FAILURE) { 
          CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Technical Failure", getApplicationContext()); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e(TAG, "Server Error:-" + error); 
        m_Dialog.dismiss(); 
        if (m_SwipeRefresh.isRefreshing()){ 
         m_SwipeRefresh.setRefreshing(false); 
        }else { 
         if(m_Dialog!=null && m_Dialog.isShowing()){ 
         m_Dialog.dismiss();} 
        } 
        if (error instanceof TimeoutError) { 
         CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Connection time out! please try again", getApplicationContext()); 
        } else if (error instanceof NetworkError) { 
         CSnackBar.getInstance().showSnackBarError(m_MainLayout, "No Internet connection", getApplicationContext()); 
        } 
       } 
      }); 

      requestQueue.add(jsonObjectRequest); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

mohit它的工作.......... – Niraj

+0

但當我按服務器請求期間的主頁按鈕.....它給我錯誤........ mDialog.dismiss()作爲空指針異常 – Niraj

+0

檢查出我編輯的代碼,它將正常工作..更安全地把該行放在Try-Catch塊中也... –

0

設置一個變量,如果刷卡刷新是怎麼回事像這樣的標記。

private boolean isRefreshing = false; 

m_SwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
    @Override 
    public void onRefresh() { 
     isRefreshing = true; // Set the swipe refresh to true 
     getWalletBalance(); 
    } 
}); 

/* Here in this method we send request to server for wallet balance */ 
private void getWalletBalance() { 
    CLoginSessionManagement s_oSessionManagement = new CLoginSessionManagement(getApplicationContext());// making object of Registartion session management 
    // retreive user data from shared preferencce........ 
    HashMap<String, String> user = s_oSessionManagement.getLoginDetails();// getting String from Regisatrtion session 
    String m_szMobileNumber = user.get(CLoginSessionManagement.s_szKEY_MOBILE).trim(); 
    String m_szEncryptedPassword = user.get(CLoginSessionManagement.s_szKEY_PASSWORD).trim(); 
    try { 
     String json; 
     // 3. build jsonObject 
     JSONObject jsonObject = new JSONObject(); 
     jsonObject.put("agentCode", m_szMobileNumber);// sending mobile no.(static right know becuse of ser side data on other is null 
     jsonObject.put("pin", m_szEncryptedPassword);// same here as said above 
     // 4. convert JSONObject to JSON to String 
     json = jsonObject.toString(); 
     Log.i(TAG, "Server request:-" + json); 
     // Add a check here 
     if(isRefreshing) m_Dialog = DialogUtils.showProgressDialog(getApplicationContext(),"Fetching wallet details..."); 
     final String s_szWalletURL = "http://metallica/getWalletBalanceInJSON"; 
     RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
     JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, s_szWalletURL, jsonObject, new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       Log.e(TAG, "Server Response:-" + response); 

       // Reset the value here 
       isRefreshing = false; 

       if (m_SwipeRefresh.isRefreshing()){ 
        m_SwipeRefresh.setRefreshing(false); 
       }else { 
        m_Dialog.dismiss(); 
       } 
       try { 
        int nResultCodeFromServer = Integer.parseInt(response.getString("resultcode")); 

        if (nResultCodeFromServer == CStaticVar.m_kTRANSACTION_SUCCESS) { 
         s_szWalletBalance = response.getString("walletbalance").trim();// get wallet balance fro response 
         String trimwalletBalance = s_szWalletBalance.substring(0, s_szWalletBalance.indexOf("."));// trim waalet balance from response. 
         CWalletDataModel.getInstance().setS_szWalletBalance(trimwalletBalance);// set wallet balance 
         // showing wallet transaction in textView.... 
         m_WalletText.setText(CWalletDataModel.getInstance().getS_szWalletBalance()); 
        } else if (nResultCodeFromServer == CStaticVar.m_kCONNECTION_NOT_AVAILABLE) { 
         CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Connection not available", getApplicationContext()); 
        } else if (nResultCodeFromServer == CStaticVar.m_kTIMED_OUT) { 
         CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Timed Out", getApplicationContext()); 
        } else if (nResultCodeFromServer == CStaticVar.m_kTECHNICAL_FAILURE) { 
         CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Technical Failure", getApplicationContext()); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e(TAG, "Server Error:-" + error); 
       m_Dialog.dismiss(); 
       if (m_SwipeRefresh.isRefreshing()){ 
        m_SwipeRefresh.setRefreshing(false); 
       }else { 
        m_Dialog.dismiss(); 
       } 
       if (error instanceof TimeoutError) { 
        CSnackBar.getInstance().showSnackBarError(m_MainLayout, "Connection time out! please try again", getApplicationContext()); 
       } else if (error instanceof NetworkError) { 
        CSnackBar.getInstance().showSnackBarError(m_MainLayout, "No Internet connection", getApplicationContext()); 
       } 
      } 
     }); 

     requestQueue.add(jsonObjectRequest); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
}