MarkOSullivan94 MarkOSullivan94 - 19 days ago 7
Android Question

Validating multiple EditTexts

Been stuck on this all night and there just doesn't seem to be an easy solution to it. I'm trying to validate all 4 of my fields to ensure that there is a value in each one of them, if there's a value in each one of them after I click the Calculate button a total will be calculated. If any of them don't have a value in them it'll return an error at every EditText which doesn't have a value and a total will not be calculated.

cal.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if( eLoan.getText().toString().length() == 0 )
{
eLoan.setError( "A value is required" );
}
else if( eWage.getText().toString().length() == 0 )
{
eWage.setError( "A value is required" );
}
else if( eGrant.getText().toString().length() == 0 )
{
eGrant.setError( "A value is required" );
}
else if( eOther.getText().toString().length() == 0 )
{
eOther.setError( "A value is required" );
}
else
convertToString();
converToDouble();
inTotal = inLoan + inWage + inGrant + inOther;
DecimalFormat currency = new DecimalFormat(".00");
TotalInNum.setText("£" + currency.format(inTotal));
}

});


I can't get my head around it, I've tried to incorporate a boolean statement to check each EditText but it didn't work either. I'm convinced there's an easier method to do this.

I'm quite new to android, self teaching myself it so I would appreciate it if people could advise me on what I'm doing wrong and maybe give me an example of what I should do.

Thanks to all who respond.

Answer

I think the problem is you're missing curlies at the last else, where the logic sits. As it is right now, only convertToString(); is part of that last else and the last four statements will execute no matter what error you're setting.

Try this:

cal.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
        boolean failFlag = false;
        // TODO Auto-generated method stub  
        if( eLoan.getText().toString().trim().length() == 0 )
        {
            failFlag = true;
            eLoan.setError( "A value is required" );
        }
        if( eWage.getText().toString().trim().length() == 0 )
        {
            failFlag = true;
            eWage.setError( "A value is required" );
        }
        if( eGrant.getText().toString().trim().length() == 0 )
        {
            failFlag = true;
            eGrant.setError( "A value is required" );
        }
        if( eOther.getText().toString().trim().length() == 0 )
        {
            failFlag = true;                
            eOther.setError( "A value is required" );
        }
        // if all are fine
        if (failFlag == false) {
            convertToString();
            converToDouble();               
            inTotal = inLoan + inWage + inGrant + inOther;
            DecimalFormat currency = new DecimalFormat(".00");
            TotalInNum.setText("£" + currency.format(inTotal));
        }
    }

});

This code will set more than one error, if more exist. Yours will signal only the first found error.

Comments