I dont want to save empty data in my Note App. I have tried Everything but when I leave Edittexts empty it still saves data into my data base. what should I do?
this is my insertNote method
public boolean insertNote(Note note){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(KEY_TITLE,note.getTitle()); contentValues.put(KEY_DESCRIPTION,note.getDescription()); contentValues.put(KEY_TIME,note.getTime()); contentValues.put(KEY_DATE,note.getDate()); long insert = db.insert(NoteDataBase.TABLE_NAME, null, contentValues); if (insert == -1){ return false; }else { return true; } }
and this is the method that saves my notes in my AddNoteActivity
btn_save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Note note = new Note(); note.setTitle(edt_title.getText().toString()); note.setDescription(edt_desc.getText().toString()); note.setDate(date); note.setTime(time); noteDBAdapter = new NoteDBAdapter(getApplicationContext()); boolean success = noteDBAdapter.insertNote(note); if (success == true){ Snackbar.make(v, R.string.snack_note2,Snackbar.LENGTH_LONG).show(); }else { Snackbar.make(v, R.string.snack_note,Snackbar.LENGTH_LONG).show(); } } });
i have also tried this but this is but this doesn’t work either
btn_save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (edt_title.equals("") || edt_desc.equals("")){ Snackbar.make(v, R.string.snack_note2,Snackbar.LENGTH_LONG).show(); }else { Note note = new Note(); note.setTitle(edt_title.getText().toString()); note.setDescription(edt_desc.getText().toString()); note.setDate(date); note.setTime(time); noteDBAdapter = new NoteDBAdapter(getApplicationContext()); noteDBAdapter.insertNote(note); } } });
appreciate any help…
Advertisement
Answer
First check edittext are blank or not. Use trim() as well as @Umesh suggested.
btn_save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (edt_title.getText().toString().trim().equals("") || edt_desc.getText().toString().trim().equals("")){ Snackbar.make(v, R.string.snack_note2,Snackbar.LENGTH_LONG).show(); } else { Note note = new Note(); note.setTitle(edt_title.getText().toString()); note.setDescription(edt_desc.getText().toString()); note.setDate(date); note.setTime(time); noteDBAdapter = new NoteDBAdapter(getApplicationContext()); noteDBAdapter.insertNote(note); } } });