0

我在对话框中创建一个表单。

我使用验证表单来防止数据为空

这是我的对话

new AlertDialog.Builder(this)

                .setTitle(R.string.add_title)
                .setView(addView)
                .setPositiveButton(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                if (wrapper.getCode().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Code belum terisi");return;}
                                else if (wrapper.getAlamat().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Alamat belum terisi");return;}
                                else if (wrapper.getBatas().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Informasi batas belum terisi");return;}
                                else if (wrapper.getLat().equals("Unknown")) {CUtilities.showAlert(MainPetakTetap.this, "Latitude belum terdeteksi");return;}
                                else if (wrapper.getLon().equals("Unknown")) {CUtilities.showAlert(MainPetakTetap.this, "Longitude belum terdeteksi");return;}
                                else if (wrapper.getLuas().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Luas belum terisi");return;}
                                else if (wrapper.getTglA().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Tanggal Awal belum terisi");return;}
                                else if (wrapper.getTglB().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Tanggal Akhir belum terisi");return;}
                                else if (wrapper.getUser().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "User Id belum terisi");return;}

                                final DatabaseConnector dbConnector = 
                                        new DatabaseConnector(MainPetakTetap.this);
                                db = new DatabaseConnector(MainPetakTetap.this);
                                dbConnector.open();
                                Cursor cursor = dbConnector.getUser(wrapper);

                                if (!cursor.moveToFirst()) {
                                    do {

                                        if (cursor!=null && cursor.getCount()==0) {
                                            db.processAddB(wrapper);
                                            constantsCursor.requery();
                                        }



                                    } while (cursor.moveToNext());
                                }
                                else{
                                    CUtilities.showAlert(MainPetakTetap.this, "Code yang anda insert sudah terdaftar!"); return;
                                }
                                if (cursor != null && !cursor.isClosed()) {
                                    cursor.close();
                                }
                                if (db != null) {
                                    db.close();
                                }


                                new AlertDialog.Builder(MainPetakTetap.this)
                                .setMessage("Data telah Tersimpan di aplikasi. Kirim ke Server?")
                                .setTitle("Pesan")

                                //.setView(addView)
                                .setPositiveButton(R.string.kirim,
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog,
                                                    int whichButton) {

                                                String code = wrapper.getCode();
                                                String alamat = wrapper.getAlamat();
                                                String batas = wrapper.getBatas();
                                                String lat = wrapper.getLat();
                                                String lon = wrapper.getLon();
                                                String luas = wrapper.getLuas();
                                                String tglA = wrapper.getTglA();
                                                String tglB = wrapper.getTglB();
                                                String userId = wrapper.getUser();
                                                new ProcessSend().execute(code, alamat, batas, lat, lon, luas, tglA, tglB, userId);
                                                //saveContact(code);
                                            }

                            })

                    .setNegativeButton(R.string.batal,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    // stop the gps when cancel
                                    lman.removeUpdates(locaListener);
                                }
                            }).show();

                            }
                        })

                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                // stop the gps when cancel
                                lman.removeUpdates(locaListener);
                            }
                        }).show();

这是验证

if (wrapper.getCode().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Code belum terisi");return;}
                                else if (wrapper.getAlamat().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Alamat belum terisi");return;}
                                else if (wrapper.getBatas().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Informasi batas belum terisi");return;}
                                else if (wrapper.getLat().equals("Unknown")) {CUtilities.showAlert(MainPetakTetap.this, "Latitude belum terdeteksi");return;}
                                else if (wrapper.getLon().equals("Unknown")) {CUtilities.showAlert(MainPetakTetap.this, "Longitude belum terdeteksi");return;}
                                else if (wrapper.getLuas().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Luas belum terisi");return;}
                                else if (wrapper.getTglA().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Tanggal Awal belum terisi");return;}
                                else if (wrapper.getTglB().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "Tanggal Akhir belum terisi");return;}
                                else if (wrapper.getUser().length()==0) {CUtilities.showAlert(MainPetakTetap.this, "User Id belum terisi");return;}

我尝试使用此代码并验证工作,但对话框被关闭。

我尝试return;在验证中删除,但数据直接处理而不验证。

我想保持对话框,直到数据得到验证。怎么做?

请问我真的需要帮助

4

1 回答 1

0

这是我为保留验证对话框所做的工作

table_array=new String[numOfTables+1];
    for(int i=1; i < numOfTables+1; i++)
    {
        table_array[0]="Select";
        table_array[i]=String.valueOf(i);
    }

    // populate spinner
    spinner=new Spinner(context);
    spinner.setAdapter( new   ArrayAdapter(context,android.R.layout.simple_dropdown_item_1line, table_array));

    final AlertDialog d = new AlertDialog.Builder(context)
    .setView(spinner)
    .setTitle("Select table number")
    .setPositiveButton(android.R.string.ok,
            new Dialog.OnClickListener() {
                @Override
                public void onClick(DialogInterface d, int which) {

                }
            })
    .setNegativeButton(android.R.string.cancel, null)
    .create();

    d.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

            Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
            b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    if(!spinner.getSelectedItem().toString().equals("Select"))
                        {
                               d.dismiss();


                        }
                        else
                            {
                                Toast.makeText(context, "Please select the table number", 0).show();
                            }
                }
            });
        }
    });


    d.show();
于 2013-04-11T10:59:10.527 回答