在android中的条件中插入对话框

时间:2013-07-26 10:00:38

标签: android sql-server if-statement dialog

我创建了一个将数据插入sql server的应用程序。连接服务器没有问题。但是当用户输入已插入数据库的相同数据时,我想使用一个对话框。但我不知道它为什么不能正常工作...

我的代码在这里.....

public void insert(){
            final String fname=name.getText().toString();
            final String fcity=city.getText().toString();
             ResultSet rs;
             try{
             Statement statement1=connect.createStatement();
             rs=statement1.executeQuery("SELECT * FROM FORM1");
             List<Map<String,String>>data1=null;
             data1=new ArrayList<Map<String,String>>();

             while(rs.next()){
                 Map<String,String>datanum1=new HashMap<String,String>();
                 datanum1.put("a", rs.getString("NAME"));
                 datanum1.put("b", rs.getString("CITY"));
                 data1.add(datanum1);
             }
             if(data1.contains(fname)!=false){
                 AlertDialog.Builder adb=new AlertDialog.Builder(context);
                 adb.setTitle("My Records");
                 adb.setMessage(fname+" Already exist... do u want to overwrite???");
                 adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        try{
                         Statement statement2=connect.createStatement();
                        statement2.executeUpdate("UPDATE FORM1 SET NAME='"+fname+"',CITY='"+fcity+"' WHERE NAME='fname'");
                        }catch(Exception e){
                            Log.e("ERROR", e.getMessage());
                        }

                    }
                });
                 adb.setNegativeButton("No", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                });
                 AlertDialog adbc=adb.create();
                 adbc.show();
             }
             else{
                 Statement statement3=connect.createStatement();
                 statement3.executeUpdate("INSERT INTO FORM1(NAME,CITY) VALUES('"+name.getText().toString()+"','"+city.getText().toString()+"')");
             }

             }catch(Exception e){
                Log.e("ERROR", e.getMessage()); 
             }
         }

如果有人请告诉我我做了什么错误以及使用的代码是什么......

1 个答案:

答案 0 :(得分:0)

确保仅在UI线程中显示对话框。如果它不是从ui Thread调用,只需使用runOnUiThread(..)方法,如下所示:

-----

if(data1.contains(fname)!=false){

 runOnUiThread(new Runnable() {

            @Override
            public void run() {

                 AlertDialog.Builder adb=new AlertDialog.Builder(context);
                 adb.setTitle("My Records");
                 adb.setMessage(fname+" Already exist... do u want to overwrite???");
                 adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        try{
                         Statement statement2=connect.createStatement();
                        statement2.executeUpdate("UPDATE FORM1 SET NAME='"+fname+"',CITY='"+fcity+"' WHERE NAME='fname'");
                        }catch(Exception e){
                            Log.e("ERROR", e.getMessage());
                        }

                    }
                });
                 adb.setNegativeButton("No", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                });
                 AlertDialog adbc=adb.create();
                 adbc.show();
    }
        });
         }