0

Heres the code that i am using

public void onClick(View v)
{
    // TODO Auto-generated method stub
    if (v == btnAdd)
    {
        try
        {
            NewAccount account = new NewAccount();
            account.setFacultyID(saveFacultyID.getText().toString());
            account.setFname(saveFname.getText().toString());
            account.setLname(saveLname.getText().toString());
            account.setPassword(savePassword.getText().toString());
            account.setRetype(saveRetype.getText().toString());

            db.AddTeacher(account);

            Toast toast = Toast.makeText(this, "New Record Saved", Toast.LENGTH_SHORT);
            toast.show();

        }catch(Exception e)
        {
            if((saveFacultyID.getText() ==null) ||
                    (saveFname.getText()==null) ||
                    (saveLname.getText()==null)||
                    (savePassword.getText()==null)||
                    (saveRetype.getText()==null))
            {
            Toast toast = Toast.makeText(this, "All Fields are Required", Toast.LENGTH_SHORT);
            toast.show();
            }
        }
    }
}
  1. It keeps saving data even if one field is empty or even all fields are empty 2.Always notify the user "New Record Save"

Where am i wrong?

Cedys Mapa
  • 41
  • 4
  • Data won't be saved because _you_ created logic which prevents that from happening. That being said, did you actually add code which checks for null/empty fields? – Tim Biegeleisen Feb 19 '17 at 14:18
  • `null` and `""` are different, for starters. `"".toString()` works and returns `""` because `""` is not `null`. – Dan Getz Feb 19 '17 at 15:51

2 Answers2

0

You put your validation checks in the wrong place in your code. I've added a new method empty for your reference.

public void onClick(View v)
{
    // TODO Auto-generated method stub
    if (v == btnAdd)
    {
        try
        {

            if((!empty(saveFacultyID.getText())) ||
                    (saveFname.getText()==null) ||
                    (saveLname.getText()==null)||
                    (savePassword.getText()==null)||
                    (saveRetype.getText()==null))
            {
              Toast toast = Toast.makeText(this, "All Fields are Required", Toast.LENGTH_SHORT);
              toast.show();

            }else{
              NewAccount account = new NewAccount();
              account.setFacultyID(saveFacultyID.getText().toString());
              account.setFname(saveFname.getText().toString());
              account.setLname(saveLname.getText().toString());
              account.setPassword(savePassword.getText().toString());
              account.setRetype(saveRetype.getText().toString());

              db.AddTeacher(account);

              Toast toast = Toast.makeText(this, "New Record Saved", Toast.LENGTH_SHORT);
              toast.show();
            }
        }catch(Exception e)
        {
            //handle exception if any...
        }
    }
}

public boolean empty( final String s ) {
   // Null-safe, short-circuit evaluation.
   return s == null || s.trim().isEmpty();
}
Community
  • 1
  • 1
ultrajohn
  • 2,527
  • 4
  • 31
  • 56
0

You did not put validation for all of you field. Write validation first. Follow this way -

    public void onClick(View v)
    {
        // TODO Auto-generated method stub
      if (v == btnAdd){
         if(saveFacultyID.getText().toString() != "" 
    || !saveFacultyID.getText().toString().isEmpty() 
                        || saveFname.getText().toString() != "" 
    || !saveFname.getText().toString().isEmpty()
                        || savePassword.getText().toString() != "" 
    || !savePassword.getText().toString().isEmpty()
                        || saveRetype.getText().toString() != "" 
    || !saveRetype.getText().toString().isEmpty()){

            NewAccount account = new NewAccount();
            account.setFacultyID(saveFacultyID.getText().toString());
            account.setFname(saveFname.getText().toString());
            account.setLname(saveLname.getText().toString());
            account.setPassword(savePassword.getText().toString());
            account.setRetype(saveRetype.getText().toString());

            db.AddTeacher(account);

            Toast toast = Toast.makeText(this, "New Record Saved",           Toast.LENGTH_SHORT);
            toast.show();
            }else{
                 Toast toast = Toast.makeText(this, "All Fields are        Required", Toast.LENGTH_SHORT);
}
        }
    }

And the best practice is to check the id of view, not the view directly. Replace the line if (v == btnAdd) to if (v.getId() == R.id.your_view_id)

AGM Tazim
  • 2,213
  • 3
  • 16
  • 25