簡體   English   中英

在Android中“刷卡刷新”時如何關閉進度對話框?

[英]How to dismiss Progress Dialog when Swipe to refresh is Refreshing in android?

我有一個應用程序,其中在onCreate內向服務器發送請求,並在再次向下滑動時添加了SwipeRefresh ,該請求已發送到服務器。

問題是,當我向下滑動時,會顯示不需要的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();
    }
}
        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();
        }
    }

設置一個變量以標記是否正在進行刷新。

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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM