这是我的主要代码:
buyButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyApplication myApplication = (MyApplication) getApplication();
userID = myApplication.getUserID();
// 布局文件转换为view对象
LayoutInflater inflaterDl = LayoutInflater
.from(BuyGoodsDetailActivity.this);
LinearLayout layout = (LinearLayout) inflaterDl.inflate(
R.layout.dialog, null);
// 对话框
final Dialog dialog = new AlertDialog.Builder(
BuyGoodsDetailActivity.this).create();
dialog.show();
dialog.getWindow().setContentView(layout);
TextView dialogTitle, dialogMessage, dialogMessageMid,
dialogMessageRight, leftButtonText, rightButtonText;
dialogTitle = (TextView) layout.findViewById(R.id.dialog_title);
dialogMessage = (TextView) layout.findViewById(R.id.dialog_msg);
dialogMessageMid = (TextView) layout
.findViewById(R.id.dialog_msg2);
dialogMessageRight = (TextView) layout
.findViewById(R.id.dialog_msg3);
// leftButtonText = (TextView)
// layout.findViewById(R.id.dialog_leftBtnTxt);
rightButtonText = (TextView) layout
.findViewById(R.id.dialog_rightBtnTxt);
if (userID == 0) {
Log.i(Tag, "userID == "+String.valueOf(userID));
dialogTitle.setText(getResources().getString(
R.string.dialog_login_title));
dialogMessage.setText(getResources().getString(
R.string.dialog_login_message));
rightButtonText.setText(getResources().getString(
R.string.dialog_login_rightButton));
// 取消按钮
RelativeLayout buttonLeft = (RelativeLayout) layout
.findViewById(R.id.leftBtn);
buttonLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
// 确定按钮
RelativeLayout buttonRight = (RelativeLayout) layout
.findViewById(R.id.rightBtn);
buttonRight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
dialogIntent = new Intent();
dialogIntent.setClass(BuyGoodsDetailActivity.this,
LoginActivity.class);
startActivity(dialogIntent);
}
});
} else {
Log.i(Tag, "userID == "+String.valueOf(userID));
new get_user_id_money().execute();
Log.i(Tag, "the totalPrice original is: " + totalPrice);
while(totalPrice == null){
}
Log.i(Tag, "the totalPrice is: " + totalPrice);
Log.i(Tag, "the finalPriceTwoDecimal: " + finalPriceTwoDecimal);
if (userTotalPrice < finalPriceTwoDecimal) {
dialogTitle.setText(getResources().getString(
R.string.dialog_less_money_title));
dialogMessage.setText(getResources().getString(
R.string.dialog_less_money_message));
float extraPay = finalPriceTwoDecimal - userTotalPrice;
dialogMessageMid.setText(String.valueOf(extraPay));
dialogMessageRight.setText(getResources().getString(
R.string.dialog_less_money_message_more));
rightButtonText.setText(getResources().getString(
R.string.dialog_less_money_rightButton));
// 取消按钮
RelativeLayout buttonLeft = (RelativeLayout) layout
.findViewById(R.id.leftBtn);
buttonLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
// 确定按钮
RelativeLayout buttonRight = (RelativeLayout) layout
.findViewById(R.id.rightBtn);
buttonRight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
dialogIntent = new Intent();
dialogIntent.setClass(
BuyGoodsDetailActivity.this,
UserPurseActivity.class);
startActivity(dialogIntent);
}
});
} else {
dialogTitle.setText(getResources().getString(
R.string.dialog_buy_title));
dialogMessage.setText(getResources().getString(
R.string.dialog_buy_message));
dialogMessageMid.setText(String
.valueOf(finalPriceTwoDecimal));
dialogMessageRight.setText(getResources().getString(
R.string.dialog_less_money_message_more));
rightButtonText.setText(getResources().getString(
R.string.dialog_buy_rightButton));
// 取消按钮
RelativeLayout buttonLeft = (RelativeLayout) layout
.findViewById(R.id.leftBtn);
buttonLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
// 确定按钮
RelativeLayout buttonRight = (RelativeLayout) layout
.findViewById(R.id.rightBtn);
buttonRight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
new buy_product().execute();
}
});
}
}
}
});
和AsyncTask是: class get_user_id_money扩展了AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
String userIDString = String.valueOf(userID);
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userID", userIDString));
MyApplication myApplication = (MyApplication) getApplication();
String url_get_user_id_money = myApplication.getIP()
+ "vendingmachine/user_id_money.php";
// getting JSON Object
JSONObject json = jsonParser.makeHttpRequest(url_get_user_id_money,
"GET", params);
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
totalPrice = json.getString("totalPrice");
Log.i(Tag, "totalPrice is: " + totalPrice);
userTotalPrice = Float.parseFloat(totalPrice);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
运行此代码时,请执行以下操作:
new get_user_id_money().execute();
Log.i(Tag, "the totalPrice original is: " + totalPrice);
while(totalPrice == null){
}
Log.i(Tag, "the totalPrice is: " + totalPrice);
Log.i(Tag, "the finalPriceTwoDecimal: " + finalPriceTwoDecimal);
if (userTotalPrice < finalPriceTwoDecimal) {
totalprice是字符串类型
the userTotalPrice = String.valueOf(totalprice)
但当我运行此代码时,在新的get_user_id_money()之前执行();完成后,主线程仍在运行。
所以我在对话框中得到了这个答案:-106.79995
这意味着
userTotalPrice < finalPriceTwoDecimal
然后运行if语句,userTotalPrice得到答案109.95,finalPriceTwoDecimal得到答案:3.15,
float extraPay = finalPriceTwoDecimal - userTotalPrice;
dialogMessageMid.setText(String.valueOf(extraPay));
所以答案是-106.79995
我使用while语句导致死循环,但它不起作用,我该怎么办?
我知道while语句也是一个非常糟糕的主意%&gt; _&lt;%
答案 0 :(得分:0)
我建议您将所有内容移入if语句&#34; if(userTotalPrice&lt; finalPriceTwoDecimal)&#34;进入asynctask的onPostExecute方法。 onPostExecute方法用于更新UI。 while循环不是一个好主意。由于您在asynctask中获取数据,因此您应该检查相同的任务。希望这会有所帮助。