use of org.javarosa.core.model.data.DecimalData in project collect by opendatakit.
the class ExDecimalWidget method setBinaryData.
/**
* Allows answer to be set externally in {@link FormEntryActivity}.
*/
@Override
public void setBinaryData(Object answer) {
DecimalData decimalData = ExternalAppsUtils.asDecimalData(answer);
this.answer.setText(decimalData == null ? null : decimalData.getValue().toString());
}
use of org.javarosa.core.model.data.DecimalData in project collect by opendatakit.
the class RangeWidgetTest method getAnswerShouldReflectActualValueSetViaSeekBar.
@Test
public void getAnswerShouldReflectActualValueSetViaSeekBar() {
W widget = getWidget();
assertNull(widget.getAnswer());
int progress = Math.abs(random.nextInt()) % widget.getElementCount();
widget.onProgressChanged(widget.getSeekBar(), progress, true);
BigDecimal actualValue;
if (rangeStart.compareTo(rangeEnd) == -1) {
actualValue = rangeStart.add(new BigDecimal(progress).multiply(rangeStep));
} else {
actualValue = rangeStart.subtract(new BigDecimal(progress).multiply(rangeStep));
}
IAnswerData answer = widget.getAnswer();
IAnswerData compareTo;
if (answer instanceof DecimalData) {
compareTo = new DecimalData(actualValue.doubleValue());
} else {
compareTo = new IntegerData(actualValue.intValue());
}
assertEquals(answer.getDisplayText(), compareTo.getDisplayText());
}
use of org.javarosa.core.model.data.DecimalData in project javarosa by opendatakit.
the class XFormAnswerDataParser method getAnswerData.
public static IAnswerData getAnswerData(String text, int dataType, QuestionDef q) {
String trimmedText = text.trim();
if (trimmedText.length() == 0)
trimmedText = null;
switch(dataType) {
case Constants.DATATYPE_NULL:
case Constants.DATATYPE_UNSUPPORTED:
case Constants.DATATYPE_TEXT:
case Constants.DATATYPE_BARCODE:
case Constants.DATATYPE_BINARY:
return new StringData(text);
case Constants.DATATYPE_INTEGER:
try {
return (trimmedText == null ? null : new IntegerData(Integer.parseInt(trimmedText)));
} catch (NumberFormatException nfe) {
return null;
}
case Constants.DATATYPE_LONG:
try {
return (trimmedText == null ? null : new LongData(Long.parseLong(trimmedText)));
} catch (NumberFormatException nfe) {
return null;
}
case Constants.DATATYPE_DECIMAL:
try {
return (trimmedText == null ? null : new DecimalData(Double.parseDouble(trimmedText)));
} catch (NumberFormatException nfe) {
return null;
}
case Constants.DATATYPE_CHOICE:
Selection selection = getSelection(text, q);
return (selection == null ? null : new SelectOneData(selection));
case Constants.DATATYPE_CHOICE_LIST:
return new SelectMultiData(getSelections(text, q));
case Constants.DATATYPE_DATE_TIME:
Date dt = (trimmedText == null ? null : DateUtils.parseDateTime(trimmedText));
return (dt == null ? null : new DateTimeData(dt));
case Constants.DATATYPE_DATE:
Date d = (trimmedText == null ? null : DateUtils.parseDate(trimmedText));
return (d == null ? null : new DateData(d));
case Constants.DATATYPE_TIME:
Date t = (trimmedText == null ? null : DateUtils.parseTime(trimmedText));
return (t == null ? null : new TimeData(t));
case Constants.DATATYPE_BOOLEAN:
if (trimmedText == null) {
return null;
} else {
if (trimmedText.equals("1")) {
return new BooleanData(true);
}
if (trimmedText.equals("0")) {
return new BooleanData(false);
}
return trimmedText.equals("t") ? new BooleanData(true) : new BooleanData(false);
}
case Constants.DATATYPE_GEOPOINT:
if (trimmedText == null) {
return new GeoPointData();
}
try {
UncastData uncast = new UncastData(trimmedText);
// silly...
GeoPointData gp = new GeoPointData();
return gp.cast(uncast);
} catch (Exception e) {
return null;
}
case Constants.DATATYPE_GEOSHAPE:
if (trimmedText == null) {
return new GeoShapeData();
}
try {
UncastData uncast = new UncastData(trimmedText);
// silly...
GeoShapeData gs = new GeoShapeData();
return gs.cast(uncast);
} catch (Exception e) {
return null;
}
case Constants.DATATYPE_GEOTRACE:
if (trimmedText == null) {
return new GeoTraceData();
}
try {
UncastData uncast = new UncastData(trimmedText);
// silly...
GeoTraceData gl = new GeoTraceData();
return gl.cast(uncast);
} catch (Exception e) {
return null;
}
default:
return new UncastData(trimmedText);
}
}
use of org.javarosa.core.model.data.DecimalData in project collect by opendatakit.
the class DecimalWidgetTest method getNextAnswer.
@NonNull
@Override
public DecimalData getNextAnswer() {
// Need to keep under 15 digits:
double d = random.nextDouble();
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
// The Widget internally truncatest this further.
nf.setMaximumFractionDigits(13);
nf.setMaximumIntegerDigits(13);
nf.setGroupingUsed(false);
String formattedValue = nf.format(d);
return new DecimalData(Double.parseDouble(formattedValue));
}
use of org.javarosa.core.model.data.DecimalData in project collect by opendatakit.
the class ExDecimalWidgetTest method getNextAnswer.
@NonNull
@Override
public DecimalData getNextAnswer() {
// Need to keep under 15 digits:
double d = random.nextDouble();
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
// The Widget internally truncatest this further.
nf.setMaximumFractionDigits(13);
nf.setMaximumIntegerDigits(13);
nf.setGroupingUsed(false);
String formattedValue = nf.format(d);
return new DecimalData(Double.parseDouble(formattedValue));
}
Aggregations