0

My code:

if(!"".equals(et1.getText().toString())){
   Toast.makeText(getActivity(), "Please enter text1", Toast.LENGTH_SHORT).show();
} else if(!"".equals(et2.getText().toString())){
   Toast.makeText(getActivity(), "Please enter text2", Toast.LENGTH_SHORT).show();
} else if(!"".equals(et3.getText().toString())){
   Toast.makeText(getActivity(), "Please enter text3", Toast.LENGTH_SHORT).show();
} else if(!"".equals(et4.getText().toString())){
   Toast.makeText(getActivity(), "Please enter text4", Toast.LENGTH_SHORT).show();
} else if(!"".equals(et5.getText().toString())){
   Toast.makeText(getActivity(), "Please enter text5", Toast.LENGTH_SHORT).show();
} 

//Totally 13 fields, so this will go near 13 checking. So just I want to know is there any possibility to check with one statement and find which filed is leaved as empty.

Thank you.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128

5 Answers5

5

Store the objects (et1, 2, 3, ...) in an array, and then iterate on it.

Antzi
  • 12,831
  • 7
  • 48
  • 74
  • While it works, i think it is against the OOP approach. When you need to use many `if` statements like OP, then consider polymorphism to solve the task. –  Oct 24 '13 at 06:14
  • I just want to check within one execution, Anyway thanks for your help. +1 added. – Gunaseelan Oct 24 '13 at 07:12
1

You can Add a textwatcher to your fields. see the snippet below.

private class GenericTextWatcher implements TextWatcher {

    private GenericTextWatcher() {

    }

    public void afterTextChanged(Editable editable) {

       if(editable.toString().equals(""))
       {
           Toast.makeText(UrActivity.this, "ur toast", Toast.LENGTH_LONG).show();
       }
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
}

and then register textlistener.

editext.addTextChangedListener(new GenericTextWatcher());

validating it on actual keypress will be faster then clubbing the checking later.

tharindu_DG
  • 8,900
  • 6
  • 52
  • 64
hepizoj
  • 243
  • 4
  • 9
1

store it in a List

for (EditText et : etList) {
    if(!"".equals(et.getText().toString()))
}
chip
  • 1,779
  • 1
  • 22
  • 34
0

I would implement view classes (in Model-View-Controller notation) that represent the necessary controls and would use polymorphism to avoid these if statements.

UPDATE:

As I understand there is shown an input validation mechanism. I would do the following.

My explanation could be more general than it is expected for your particular task. But I think it will help you in future. And please note I am not too proficient with Android API, so you may need to adjuct my references to some Android API classes, for instance a class, that represents a field.

So, the first step. Declare the field prototype:

abstract FieldPrototype extends AndroidFieldClass {

    /**
    * Checks field contents and returns true if field is ok, otherwise shows popup validation message and returns false.
    * @return see method description.
    */
    public abstract boolean checkField();

}


class Et1Field extends FieldPrototype {

    private String value;

    public Et1Field() {
        super();
        // your initialization code here
    }

    public boolean checkField() {
        if (value == null || "".equals(value)) {
            // show validation message specific for Et1Field class instance.
            return false;
        }

        return true;
    }
}

// do the same steps for Et2Field .. Et13Field if those fields are different, 
// and implement specific validation functionality for every kind of a field.

In your screen class keep an array of all fields displayed on this screen. On save event in your screen, send save event to all field instances. Every field will invoke it's own checkField() method and validate it's own contents, without showing particular field implementation outside. And if a field validation failed you just stop and let a user to correct a field contents.

0

make one function to check blank and called it once like this...

    checkBlank(et1.getText().toString()), 1);
checkBlank(et2.getText().toString()), 2);
checkBlank(et3.getText().toString()), 3);..and so on

here is function..

private void checkBlank(String edt,int pos) {
    // TODO Auto-generated method stub
    if(edt.equals(""))
    {
        Toast.makeText(getApplicationContext(), "Please enter text"+pos, Toast.LENGTH_SHORT).show();
    }

}

Godd Luck:)

Kalpesh Lakhani
  • 1,003
  • 14
  • 28