我正在尝试验证表单,以便空白条目不会存储在数据库中。 代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.second);
View rootView = inflater.inflate(R.layout.addevent, container, false);
// Edit Text
ineventname = (EditText) rootView.findViewById(R.id.editText1);
mEdit = (EditText)rootView.findViewById(R.id.editText6);
mEdit1= (EditText)rootView.findViewById(R.id.editText7);
ineventwebsite = (EditText) rootView.findViewById(R.id.editText2);
incollegename = (EditText) rootView.findViewById(R.id.editText3);
ingooglemap = (EditText) rootView.findViewById(R.id.editText5);
inlocation = (EditText) rootView.findViewById(R.id.editText4);
istartdate = (EditText) rootView.findViewById(R.id.editText6);
ienddate = (EditText) rootView.findViewById(R.id.editText7);
Button button1 = (Button) rootView.findViewById(R.id.button1);
spinner = (Spinner)rootView.findViewById(R.id.spinner1);
category = new String[] { "Cultural", "Technical",
"Sports"};
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item,category
);
spinner.setAdapter(adapter);
mEdit.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
// creating new product in background thread
f=1;f1=0;
new DatePickerDialog(getActivity(), date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
mEdit1.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
// creating new product in background thread
f1=1;f=0;
new DatePickerDialog(getActivity(), date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
icategory=spinner.getSelectedItem().toString();}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
// @Override
});
// button click event
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
//boolean r=validate();
// Log.d("check",q);
Log.d("check","Hello"+ineventname.getText().toString());
if(ineventname.getText().toString()==""||ineventwebsite.getText().toString()==""||incollegename.getText().toString()==""||ingooglemap.getText().toString()==""||inlocation.getText().toString()==""||istartdate.getText().toString()==""||ienddate.getText().toString()==""){
AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
builder1.setMessage("Please fill the fields");
builder1.setCancelable(false);
builder1.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
else{
new CreateNewProduct().execute();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, new details1());
ft.commit();
}
}
});
return rootView;
}
我使用if(ineventname.getText().toString()==""
来验证表单的输入。但是空白值仍然存储在数据库中。怎么能解决这个问题?
答案 0 :(得分:1)
因为这不是验证字符串的方式。它应该是
if(ineventname.getText().toString().equals(""))
答案 1 :(得分:1)
使用
ineventname.getText().toString().equalsIgnoreCase("")
ineventname.getText().toString()==""