how do i test if the field isn’t filled up by the user? i want to show a prompt that they have to fill up the fields completely. but this code still accepts even if the name is null and the number is entered.
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
You tested your string with null.When text box is empty String name is not null, it is an empty string.So compare it like below