I have been trying to create to optimize my android code by creating an input validation method then calling it but it doesn’t work. I have tried debugging but nothing.
This is what i had the first time and it was working:
String s1 =startTime1.getText().toString();
if(TextUtils.isEmpty(startTime1.getText())){
s1 = "00:00";
}
String e1 = endTime1.getText().toString();
if(TextUtils.isEmpty(endTime1.getText())){
e1 = "00:00";
}
but after extracting the validation into this method and calling it each time the user enters input nothing seems to work.
public void emptyInputValidation(EditText time, String timeToString){
if(TextUtils.isEmpty(time.getText())){
timeToString = "00:00";
}
}
compute.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
try{
String s1 =startTime1.getText().toString();
emptyInputValidation(startTime1, s1);
String e1 = endTime1.getText().toString();
emptyInputValidation(endTime1, e1);
}
catch (ParseException e){
e.printStackTrace();
}
}
thanks in advance.
In
timeToString = "00:00";of your function you are reassigning the parameter to"00:00", which is not reflected outside of the method. Here is the corrected code:Then simply call
String s1 = emptyInputValidation(startTime1);