3
saveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            questionStr = String.valueOf(questionET.getText());
            answerOneStr = String.valueOf(ansOneET.getText());
            answerTwoStr = String.valueOf(ansTwoET.getText());
            answerThreeStr = String.valueOf(ansThreeET.getText());
            answerFourStr = String.valueOf(ansFourET.getText());
            correctAnswerStr = String.valueOf(correctAnsET.getText());
            if (questionStr == null || answerOneStr == null || answerTwoStr == null || correctAnswerStr == null) {
                Toast.makeText(getApplicationContext(), "Please Fill Fields ", Toast.LENGTH_SHORT).show();
            } else {
                questionManagerObject.InsertIntoQuestions(questionStr, answerOneStr, answerTwoStr, answerThreeStr, answerFourStr, correctAnswerStr, questionTableSubjectId);
                Toast.makeText(getApplicationContext(), "Question Saved ", Toast.LENGTH_SHORT).show();
            }
        }
    });

this is the save button code ...i tried to change null to empty "" but there is no changes

  • 1
    Perhaps this will shed some light? http://stackoverflow.com/questions/531779/comparing-a-string-with-the-empty-string-java – karina Apr 29 '16 at 01:21
  • 1
    android sdk provides `TextUtils`: It has `isEmpty()` method. It is simple String null checker. try this `TextUtils.isEmpty(questionStr)` at `if` statements. – myoungjin Apr 29 '16 at 01:38
  • no , it does not help , if the user enter one space it will be saved – Eslam Roshdy Apr 29 '16 at 01:42

3 Answers3

1

Try this method:

private boolean isEmpty(String str)
{
    if (str == null || str.trim().isEmpty())
    {
        return true;
    }
    return false;
}

then use it in your code:

if (isEmpty(questionStr) || isEmpty(answerOneStr) ||  
    isEmpty(answerTwoStr) || isEmpty(answerThreeStr) || 
    isEmpty(answerFourStr) ||isEmpty(correctAnswerStr))
{
   // your code
} 
Hadas Kaminsky
  • 1,285
  • 1
  • 16
  • 39
  • 1
    thank you very very very much , your answer helped me – Eslam Roshdy Apr 29 '16 at 02:05
  • I would use the standard function instead: `TextUtils.isEmpty()`. See [doc](http://developer.android.com/intl/es/reference/android/text/TextUtils.html#isEmpty(java.lang.CharSequence)). The input string should be trimmed though. – kazbeel Apr 29 '16 at 06:16
0

Use String.trim() to make sure it's not a space then move on to your conditional.

trim whitespace from a string?

Community
  • 1
  • 1
0

Replace your empty checking by this:

 if (questionStr == null && answerOneStr == null 
             && answerTwoStr == null && correctAnswerStr == null) {
   Toast.makeText(getApplicationContext(), "Please Fill Fields ", Toast.LENGTH_SHORT).show();
}
sravs
  • 330
  • 2
  • 14
Lips_coder
  • 686
  • 1
  • 5
  • 17