首页 > 解决方案 > 如果先前活动有返回数据以通知用户,如何创建警报对话框?

问题描述

我正在尝试将警报对话框添加到我的项目中,以通知用户是否有以前活动的返回数据,但是在我的方法中,if 语句有问题或缺少某些东西,即使没有返回数据,警报对话框总是会弹出任何建议或想法将不胜感激。

//This part is my method
public void dataReceive(){ 
         //requesting data from server
         //adding parameters
         RequestParams params = new RequestParams(); 
         SessionManager sessionManager = new SessionManager(this);
         HashMap<String, String> user = sessionManager.getUserDetails();
        if (user != null) {
            String uid = user.get(SessionManager.USER_ID);
            params.put("user_id", uid);
        }
        try{
            String name = user.get(SessionManager.KEY_NAME);

            username = name;

        }catch (Exception e){

        }
        //This part is my approach 
        Bundle tokens = this.getIntent().getExtras();
        if(tokens != null) {
            final PendingRequestPojo pendingRequestPojo = new PendingRequestPojo();
            responseid = tokens.getString("responseid");
            requestid = tokens.getString("requestid");
            //Toast.makeText(getApplicationContext(), "responseid :" + responseid + "\nrequestid : " +
            //requestid, Toast.LENGTH_SHORT).show();

            alert = new AlertDialog.Builder(SplashActivity.this);
            alert.setTitle("ThirdWheel Payment Response");
            alert.setIcon(R.drawable.thinrdwheel_alert);
            alert.setMessage("Your Payment Was Successfull" + " " + username +
                    "Thank you, we Hope you can ride with us Again ");
            alert.setCancelable(true);
            alert.setPositiveButton("Thanks", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    changement();
                    Updatepayment(pendingRequestPojo.getRide_id(), "PAID");
                }
            }).show();

        }  else {

            changement();
        }
    }

标签: android

解决方案


您的代码必须如下

 public void dataReceive(){ 
         //requesting data from server
         //adding parameters
         RequestParams params = new RequestParams(); 
         SessionManager sessionManager = new SessionManager(this);
         HashMap<String, String> user = sessionManager.getUserDetails();
        if (user != null) {
            String uid = user.get(SessionManager.USER_ID);
            params.put("user_id", uid);
        }
        try{
            String name = user.get(SessionManager.KEY_NAME);

            username = name;

        }catch (Exception e){

        }
        //This part is my approach 
        Bundle tokens = this.getIntent().getExtras();
        if(tokens != null && 
tokens.containsKey("responseid") && tokens.containsKey("requestid")) {
            final PendingRequestPojo pendingRequestPojo = new PendingRequestPojo();
            responseid = tokens.getString("responseid");
            requestid = tokens.getString("requestid");
            //Toast.makeText(getApplicationContext(), "responseid :" + responseid + "\nrequestid : " +
            //requestid, Toast.LENGTH_SHORT).show();

            alert = new AlertDialog.Builder(SplashActivity.this);
            alert.setTitle("ThirdWheel Payment Response");
            alert.setIcon(R.drawable.thinrdwheel_alert);
            alert.setMessage("Your Payment Was Successfull" + " " + username +
                    "Thank you, we Hope you can ride with us Again ");
            alert.setCancelable(true);
            alert.setPositiveButton("Thanks", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    changement();
                    Updatepayment(pendingRequestPojo.getRide_id(), "PAID");
                }
            }).show();

        }  else {

            changement();
        }
    }

推荐阅读