2013-03-26 52 views
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;,但是數據直接進行處理而沒有驗證。

我想保留對話框,直到數據被驗證。怎麼做?

請我真的需要幫助

回答

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