0

Hi there I have been doing the notepad exercise and it has a really annoying problem which was fine and I was able to correct it up until exercise 3 which it now doesnt work! My problem is that currently it will allow empty inputs to create a note, really annoying. I was able to fix it using the

String taskName = mTitleText.getText().toString();
if (taskName.equals("")) {finish();}
else {addNote();}

method but when the full tutorial is completed the method doesnt work anymore. I have tried all sorts of ways of calling it but it still wont do anything, it just posts an empty note. It does register that it is empty but posts it anyway. Does anyone know how to fix this??

Thanks for your help Kris

stoff
  • 1
  • Post full code you have with comments in it what is not working. That will help you in getting valid answers. – kosa Feb 02 '12 at 16:20
  • The solution to Notepad exercise #3 features a `saveState` method which is responsible for saving/updating the note in the database. There is no `addNote` method, which I suppose you added in your solution. But `saveState` is called from different places, e.g. `onPause`. One possible reason why your workaround does not work is that `onPause` et similia in your solution are now calling your `addNote` method instead of `saveState`, thus bypassing the empty note guard. – Giulio Piancastelli Feb 02 '12 at 16:49
  • thats it!! thanks i totaly overlooked that. ur the man!! – stoff Feb 02 '12 at 17:10

2 Answers2

0

Maybe you need to use

import org.apache.commons.lang.StringUtils;

then

StringUtils.isBlank(taskName);

you'll probably have to add the library go here for help.

Community
  • 1
  • 1
John Boker
  • 82,559
  • 17
  • 97
  • 130
  • 1
    Why use Apache `StringUtils` when `TextUtils` and its `isEmpty` method seems to perform the same task and is included in the Android standard library? – Giulio Piancastelli Feb 02 '12 at 16:28
0

the String.isEmpty() function should suffice for this - Check out your Java docs

Chris
  • 865
  • 4
  • 15
  • 25
  • Thats what i thought but it doesnt appear to work with this code. infact when using eclipse the isEmpty() isnt even there and comes up as an error! – stoff Feb 02 '12 at 16:41
  • so do you have if(taskName.isEmpty()) {finish();} else {addNote();}, It also depends on what mTitleText.getText().toString(); is returning may be worth debugging it and seeing exactly what this string is being printed as there is no reason this shouldnt work. – Chris Feb 02 '12 at 16:50
  • yeah with taskName being defined using String taskName = mTitleText.getText().toString(); – stoff Feb 02 '12 at 16:52
  • can you print the mTitleText.getText().toString(); and see what its returning ? – Chris Feb 02 '12 at 16:55
  • mTitleText is just calling an EditText field, mTitleText = (EditText) findViewById (R.id.EditText_taskName); it checking it to see if anything is in it hense the .getText().toString() and up until exercise 3 it was working fine. i also have textwatcher making sure if anything is changed and the cancel button is hit then it will bring up a dialog box. should i maybe try and hook the textwatcher instead and see if that works? – stoff Feb 02 '12 at 16:59
  • You can try but this should be working, perhaps clean and build your code again perhaps Android hasnt regenerated ID's correctly! Maybe someone else can help as this method is working for me might be something elsewhere in your code. Sorry – Chris Feb 02 '12 at 17:02
  • when changing the code to a Toast to make sure that it is seeing that it is an empty field it returns correctly but when returning to main page it will still add empty note. mauybe it has something to do with the populatefields()?? – stoff Feb 02 '12 at 17:07