android - 如果先前活动有返回数据以通知用户,如何创建警报对话框?
问题描述
我正在尝试将警报对话框添加到我的项目中,以通知用户是否有以前活动的返回数据,但是在我的方法中,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();
}
}
解决方案
您的代码必须如下
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();
}
}
推荐阅读
- regex - 基于模式在python中将二进制文件分割成多个文件
- javascript - 在回文逻辑中查找边缘情况
- haskell - Haskell Beam 代码仅适用于一个模块
- ggplot2 - 摆脱ggplot中面板上的边框?
- java - 如何使用 jersey 实现输入 bean 验证?
- python - 计算数据帧的每个单元格
- java - 如何将查询结果集映射到表以外的其他实体
- php - 用于 UPDATE 的 mysqli_affected_rows 有时在全行匹配时返回 0
- reactjs - 如何在本机和后端 Node js 中加密和解密文本消息?
- swift - 如何正确处理 URLSession.shared.dataTask