Setting default value for field

Hi all,

I'm trying to create a new widget for my instance of ODK Collect which
will take a previously entered date (from an earlier date field),
perform a calculation and display the adjusted date to the user. The
user has no need to edit this date - they just need to view for now
(though I may in the future want to add the options for them to edit
it).

I used the calculate attribute on the bind node, like this:

to push the date into the widget and it displays the new calculated
date as I intended.

However when I save the form (and either view with ODK Collect, or
submit to Aggregate server), it's not sending the new (calculated)
value for /data/edd, but instead it's sending the original value from
the calculate attribute (/data/lmp).

In my widget I have the modified set/getAnswer() methods:

private void setAnswer() {
if (mPrompt.getAnswerValue() != null)
lmp = new DateTime(((Date) ((DateData)
mPrompt.getAnswerValue()).getValue()).getTime());{
edd = lmp.plusDays(280);
// some code here which displays the date on the
screen
} else {
Toast.makeText(getContext(), "Invalid input date",
Toast.LENGTH_SHORT).show();
}
}

public IAnswerData getAnswer() {
try {
return new DateData(edd.toDate());
} catch (Exception e) {
Toast.makeText(getContext(), "Invalid date",
Toast.LENGTH_SHORT).show();
}
return null;
}

The "edd" property has the recalculated date set ok (I can confirm by
displaying on screen).
So I'm a little confused what's going on. Is it:

a) Intended behaviour of the calculate attribute - I noticed that if I
set the field up as a regular date picker, it sets the default date
display date to be what I intend, but if I change this my changed date
isn't saved/submitted - so seems to me that the calculate attribute
overrides what the user enters?
b) a bug
c) (most likely!) I should be doing something different? I couldn't
find another way to pass in the date from a previous form field other
than using the calculate attribute - but maybe I missed something? Or
my handling of the get/setAnswer methods is wrong?

Any help much appreciated :slight_smile: I can send on more complete code if that
would help?
Cheers,
Alex

hey alex,

i had to do some testing, but i can confirm that calculate does
overwrite whatever it is you display in that prompt. offhand, i don't
know of a clean solution to passing data forward, but i've forwarded
your question to javarosa-developers list to see what the options are.

yaw

ยทยทยท On Sat, Oct 22, 2011 at 09:15, Alex Little wrote: > Hi all, > > I'm trying to create a new widget for my instance of ODK Collect which > will take a previously entered date (from an earlier date field), > perform a calculation and display the adjusted date to the user. The > user has no need to edit this date - they just need to view for now > (though I may in the future want to add the options for them to edit > it). > > I used the calculate attribute on the bind node, like this: > > > required="true()" /> > > to push the date into the widget and it displays the new calculated > date as I intended. > > However when I save the form (and either view with ODK Collect, or > submit to Aggregate server), it's not sending the new (calculated) > value for /data/edd, but instead it's sending the original value from > the calculate attribute (/data/lmp). > > In my widget I have the modified set/getAnswer() methods: > > private void setAnswer() { > if (mPrompt.getAnswerValue() != null) > lmp = new DateTime(((Date) ((DateData) > mPrompt.getAnswerValue()).getValue()).getTime());{ > edd = lmp.plusDays(280); > // some code here which displays the date on the > screen > } else { > Toast.makeText(getContext(), "Invalid input date", > Toast.LENGTH_SHORT).show(); > } > } > > public IAnswerData getAnswer() { > try { > return new DateData(edd.toDate()); > } catch (Exception e) { > Toast.makeText(getContext(), "Invalid date", > Toast.LENGTH_SHORT).show(); > } > return null; > } > > The "edd" property has the recalculated date set ok (I can confirm by > displaying on screen). > So I'm a little confused what's going on. Is it: > > a) Intended behaviour of the calculate attribute - I noticed that if I > set the field up as a regular date picker, it sets the default date > display date to be what I intend, but if I change this my changed date > isn't saved/submitted - so seems to me that the calculate attribute > overrides what the user enters? > b) a bug > c) (most likely!) I should be doing something different? I couldn't > find another way to pass in the date from a previous form field other > than using the calculate attribute - but maybe I missed something? Or > my handling of the get/setAnswer methods is wrong? > > Any help much appreciated :) I can send on more complete code if that > would help? > Cheers, > Alex >