android中的警报对话框

时间:2013-11-20 12:17:55

标签: android alert

在我的代码中,当我选择if- elseIf语句中提到的选项时,我想立即关闭警报对话框并启动活动。我不想要和取消按钮。我的代码工作正常(语句内部如果语句有效,但警告对话仍然存在)。谢谢你的帮助

final AlertDialog.Builder builder =
    new AlertDialog.Builder(arg0.getContext());
builder.setTitle("Favourities Management");
// TODO Auto-generated method stub
int selected = 0;

builder.setSingleChoiceItems(values, selected, new DialogInterface.OnClickListener() {

    @
    Override
    public void onClick(DialogInterface dialog, int which) {
        if (values[which] == "Select Benificiary") {
            Intent registerUser = new Intent(FinalUtilityBillPayment.this, ListViewBeneficiaryBillPayment.class);
            FinalUtilityBillPayment.this.startActivity(registerUser);
            startActivityForResult(registerUser, 1);
        } else if (values[which] == "Add Benificiary") {
            try {
                mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
                mydb.execSQL("CREATE TABLE IF  NOT EXISTS " + TABLE + " (ID INTEGER PRIMARY KEY, ReferenceNo TEXT, Mobile Text);");

                mydb.close();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Error in creating table", Toast.LENGTH_LONG).show();
            }
            try {
                mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
                mydb.execSQL("INSERT INTO " + TABLE + "(ReferenceNo, Mobile) VALUES('" + ref.getText().toString() + "','" + mob.getText().toString() + "')");
                mydb.close();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Error in inserting into table", Toast.LENGTH_LONG).show();
            }
        } else if (values[which] == "Delete Benificiary") {

            Intent registerUser = new Intent(FinalUtilityBillPayment.this, ListViewDeleteBeneficiaryBillPayment.class);
            //startActivityForResult(registerUser, 1);
            FinalUtilityBillPayment.this.startActivity(registerUser);




        }

    }
});
AlertDialog alert = builder.create();
alert.show();


}

});

4 个答案:

答案 0 :(得分:1)

我的消化是;

    final AlertDialog.Builder builder = new AlertDialog.Builder(arg0.getContext()); 
builder.setTitle("Favourities Management");    
// TODO Auto-generated method stub
int selected = 0;
builder.setSingleChoiceItems(values, 
                            selected, 
                            new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if(values[which]=="Select Benificiary"){
                                            selectBenificiary();

                                            //see more about "dialog.dismiss()" in http://developer.android.com/reference/android/app/Dialog.html#dismiss()
                                            dialog.dismiss();                                               
                                        } else if (values[which]=="Add Benificiary"){
                                            addBenificiary();

                                            //see more about "dialog.dismiss()" in http://developer.android.com/reference/android/app/Dialog.html#dismiss()
                                            dialog.dismiss();
                                        } else if (values[which]=="Delete Benificiary"){
                                            deleteBenificiary();

                                            //see more about "dialog.dismiss()" in http://developer.android.com/reference/android/app/Dialog.html#dismiss()
                                            dialog.dismiss();
                                        }

                                    }
                            });
AlertDialog alert = builder.create();
alert.show();

//Add parameter case necessary
public void selectBenificiary(){
    Intent registerUser = new Intent(FinalUtilityBillPayment.this,ListViewBeneficiaryBillPayment.class);
    //  FinalUtilityBillPayment.this.startActivity(registerUser);
    startActivityForResult(registerUser, 1);
}

//Add parameter case necessary
public void addBenificiary(){
    try{
        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        mydb.execSQL("CREATE TABLE IF  NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY, ReferenceNo TEXT, Mobile Text);");

        mydb.close();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Error in creating table", Toast.LENGTH_LONG).show();
    }

    try{
        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        mydb.execSQL("INSERT INTO " + TABLE + "(ReferenceNo, Mobile) VALUES('"+ref.getText().toString() +"','"+  mob.getText().toString()  +"')");
        mydb.close();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Error in inserting into table", Toast.LENGTH_LONG).show();
    }
}   

//Add parameter case necessary
public void deleteBenificiary(){
    Intent registerUser = new Intent(FinalUtilityBillPayment.this, ListViewDeleteBeneficiaryBillPayment.class);
    //startActivityForResult(registerUser, 1);
    FinalUtilityBillPayment.this.startActivity(registerUser);
}

答案 1 :(得分:0)

您必须关闭警报对话框。试试这个:

Dialogue.dismiss();

答案 2 :(得分:0)

试试这个... onClick()您可以看到DialogInterface dialog该对话框名称。

在调用其他活动之前使用此dialog.cancel();

dialog.cancel();

    Intent registerUser = new Intent(FinalUtilityBillPayment.this,ListViewBeneficiaryBillPayment.class);
    //FinalUtilityBillPayment.this.startActivity(registerUser);    
    startActivityForResult(registerUser, 1);

答案 3 :(得分:0)

调用.dismiss()来关闭它,也可以使用简单的Dialog来为它设置自定义布局

Dialog alert = new Dialog(context);
alert.setContentView(layoutResID);