我在对话框中创建一个表单。
我使用验证表单来防止数据为空时
这是我的对话
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 :(得分: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();