Varun Vishnoi Varun Vishnoi - 1 month ago 6
Android Question

Seterror signal is not disappearing and Search is working

I have implemented the code of set error on two edit text.


  • First to get any string without null or empty value.

  • Second to get any string without null or empty value and must have some Double value.



Code is as follows

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SearchPlaces extends Activity {
EditText place;
EditText distance;
Button search;
static String _place;
static String _distance;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

place = (EditText) findViewById(R.id.place);
distance = (EditText) findViewById(R.id.distance);
search = (Button) findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Map<String, String> validate = new HashMap<String, String>();
_place = place.getText().toString();
_distance = distance.getText().toString();
validate.put("placename", _place);
validate.put("distancenumeric", _distance);
if (!validateInputInformation(validate)) {
return;
}
if ((_place == "" && _distance == "")) {
Toast.makeText(getApplicationContext(),
"Please fill all the information",
Toast.LENGTH_SHORT).show();
} else {
_place = place.getText().toString();
_distance = distance.getText().toString();
Intent placedist = new Intent(getApplicationContext(),
MainActivity.class);
Bundle bundle = new Bundle();

bundle.putString("place", _place);
bundle.putString("distance", _distance);

placedist.putExtras(bundle);
startActivity(placedist);
}
}

private boolean validateInputInformation(
Map<String, String> validate) {

String l_place = validate.get("placename");
if (l_place == null || l_place.equals("")) {
place.setError("Please enter the Place First");
return false;
}

String l_distance = validate.get("distancenumeric");
if (l_distance == null || l_distance.equals("")) {
distance.setError("Please enter the Distance First");
return false;
}
if (!isDoubleNumber(l_distance)) {
distance.setError("Please enter Numeric Value");
return false;
}

return true;

}
private boolean isDoubleNumber (String num) {
try {
Double.parseDouble(num);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
});
}
}


after implementing that all working are going good but when I doesn't enter and click search button the error pops up in first edit-box after that when I reenter the correct values as input needed then the error notification doesn't disappear but search button works good.

Please help me so that the set-error notification can be removed from the first editbox.

Answer

Write below code line instead of if ((_place == "" && _distance == "")), it will solve your problem.

if (_place.equals("") && _distance.equals(""))
Comments