我如何测试该字段是否未被用户填写?我想提示他们必须完全填写这些字段。但即使名称为空并且输入了数字,此代码仍然接受。
public void onClick(View arg0)
{
switch(arg0.getId())
{
case R.id.bSQLUpdate:
boolean diditwork = true;
try
{
String name = sqlName.getText().toString();
String number = sqlNumber.getText().toString();
GroupDb entry = new GroupDb(ContactDb.this);
entry.open();
if(name != null && number != null)
{
if((!entry.hasDuplicateNameNumber(name, number)) )
{
entry.createEntry(name, number);
}
else
{
diditwork = false;
Dialog d = new Dialog(this);
d.setTitle("Error");
TextView tv = new TextView(this);
tv.setText("Duplicate name or number. Please try again.");
d.setContentView(tv);
d.show();
sqlName.setText("");
sqlNumber.setText("");
}
}
else
{
diditwork = false;
Dialog d = new Dialog(this);
d.setTitle("Error");
TextView tv = new TextView(this);
tv.setText("Please fill up all fields. Please try again.");
d.setContentView(tv);
d.show();
sqlName.setText("");
sqlNumber.setText("");
}
entry.close();
}
catch(Exception e)
{
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Update failed");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
sqlName.setText("");
sqlNumber.setText("");
}
finally
{
if(diditwork)
{
Dialog d = new Dialog(this);
d.setTitle("Contact list updated");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
sqlName.setText("");
sqlNumber.setText("");
}
}
break;
case R.id.bSQLOpenView:
Intent intent = new Intent();
intent.setClass(ContactDb.this, CustomListView.class);
startActivityForResult(intent, 0);
break;
}
}// end onclick