在我的一项活动中,需要进行一些计算并计算出总价。按“提交”按钮后,应显示一个警告对话框,您确定要支付卢比:XXX ...吗? XXX应该是我存储在变量中的最终价格。

在alertdialog.setTitle()中,我应该能够访问变量。

请帮我解决这个问题。

public void onPay()
    {
        getItems();
        int rate = 0;

        if(spare1_item.equals("Tyres") || qty_1.equals("Quantity"))
        {

        }
        else
        {
            //Toast.makeText(getApplicationContext(), "Now you can pay", 5000).show();
            db = this.openOrCreateDatabase("mobile_cycle_clinic", MODE_PRIVATE, null);
            c = db.rawQuery("select price from sparelist where name = '"+spare1_item+"'", null);
            if(c.moveToNext())
            {
                do{
                    price = c.getInt(c.getColumnIndex("price"));

                }
                while(c.moveToNext());
            }
            fianl1_qty = Integer.parseInt(qty_1);
            rate = rate + price * fianl1_qty;
            db.execSQL("insert into spares_items(cycle_id,item_name,quantity,total_price)values('"+cycle_id+"','"+spare1_item+"',"+fianl1_qty+","+rate+")");
            //Toast.makeText(getApplicationContext(), ""+rate, 5000).show();
        }


这里rate是一个静态变量,在另一种方法中,我应该在alertDialog.setMeaasge()中使用该变量。

    public void storeData(View v)
    {
    cycle_id = id.getText().toString();
    if(cycle_id.equals("") || cycle_id.equals("null"))
    {
        Toast.makeText(getApplicationContext(), "Please Scan Cycle",5000).show();
    }
    else
    {
    AlertDialog.Builder pauseBuild = new AlertDialog.Builder(this);
    pauseBuild.setTitle("Pay Alert");
    pauseBuild.setMessage("Do you really want to Pay..?"+rate);
    pauseBuild.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
    //time = sdf.format(new Date());
    onPay();
    finish();
    return;
    } });
    pauseBuild.setNegativeButton("No",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            // if this button is clicked, just close
            // the dialog box and do nothing
            dialog.cancel();
        }
    });
    // show it
    pauseBuild.show();
    }

最佳答案

您可以使用函数来显示或创建AlertDialog。
例如:

private void showConfirmAlertDialog(int price) {
  AlertDialog.Builder builder = new AlertDialog.Builder();
  builder.setTitle("Are you sure you want to pay rupees: " + price);
  ....
  builder.show();
}


如果您希望获得AlertDialog的实例,则可以将函数更改为private AlertDialog createConfirmAlertDialg(int price),并在函数末尾使用return builder.create();

关于android - 在Android中的AlertDialog中访问变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27060827/

10-14 18:34