0

我是一名初学者 android 开发人员。我有一个数据库表,它计算以下列:_id、fistName、lastName 和 username 设置为唯一的。

当我在我的表中添加另一个人并且用户名已经存在时,我创建了一个 AlertDialog,但是这个 AlertDialog 没有出现。

这是我的源代码:

try {
        personRepository.AddPerson(persons);
    } catch (SQLiteConstraintException ex) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        alertDialogBuilder.setTitle("User already exist!");
        alertDialogBuilder.setMessage("Choose another username!")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

有人能帮我吗?

4

2 回答 2

2

尝试这个

AlertDialog alert;
alert=new AlertDialog.Builder(YourActivity.this)
                .setCancelable(false)
                .setMessage("Message")
                .setPositiveButton("Okay", new DialogInterface.OnClickListener()
                {

                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                }).show();
于 2013-04-22T13:28:29.677 回答
0

如果您的AddPerson方法调用insertOrThrow然后更改

catch (SQLiteConstraintException ex)   

catch (SQLException ex)
于 2013-04-22T16:36:06.167 回答