2014-11-21 61 views
0

在我的一个活动中,有一些计算和总价格将被计算。按下提交按钮后,它应该显示一个警告对话框你确定要支付卢比:XXX ... ?这里XXX应该是我存储在变量中的最终价格。在Android中的AlertDialog中访问变量

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

这里率是一个静态变量,而在另一个方法我应该使用该变量在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(); 
    } 
+0

http://www.mkyong.com/android/android-custom-dialog-example/ – 2014-11-21 11:59:57

+0

并有你的邮政编码 – 2014-11-21 12:00:23

+0

我不明白最新的问题。该var是否与显示alertdialog的环境相同。如果是,那么直接访问它,如果没有,然后使静态和访问它像“Activity.Var” – therealprashant 2014-11-21 12:03:51

回答

1

您可以使用函数来显示或创建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(); 
} 

如果perfer得到AlertDialog的实例,可以将功能更改为private AlertDialog createConfirmAlertDialg(int price),并在函数结尾使用return builder.create();