Search in sources :

Example 1 with ItemsetDao

use of org.odk.collect.android.dao.ItemsetDao in project collect by opendatakit.

the class FormEntryPromptUtils method getAnswerText.

public static String getAnswerText(FormEntryPrompt fep, Context context, FormController formController) {
    IAnswerData data = fep.getAnswerValue();
    final String appearance = fep.getQuestion().getAppearanceAttr();
    if (data instanceof SelectMultiData) {
        StringBuilder b = new StringBuilder();
        String sep = "";
        for (Selection value : (List<Selection>) data.getValue()) {
            b.append(sep);
            sep = ", ";
            b.append(fep.getSelectItemText(value));
        }
        return b.toString();
    }
    if (data instanceof DateTimeData) {
        return DateTimeUtils.getDateTimeLabel((Date) data.getValue(), DateTimeUtils.getDatePickerDetails(appearance), true, context);
    }
    if (data instanceof DateData) {
        return DateTimeUtils.getDateTimeLabel((Date) data.getValue(), DateTimeUtils.getDatePickerDetails(appearance), false, context);
    }
    if (data != null && appearance != null && appearance.contains("thousands-sep")) {
        try {
            final BigDecimal answerAsDecimal = new BigDecimal(fep.getAnswerText());
            DecimalFormat df = new DecimalFormat();
            df.setGroupingSize(3);
            df.setGroupingUsed(true);
            df.setMaximumFractionDigits(Integer.MAX_VALUE);
            // Use . as decimal marker for consistency with DecimalWidget
            DecimalFormatSymbols customFormat = new DecimalFormatSymbols();
            customFormat.setDecimalSeparator('.');
            if (df.getDecimalFormatSymbols().getGroupingSeparator() == '.') {
                customFormat.setGroupingSeparator(' ');
            }
            df.setDecimalFormatSymbols(customFormat);
            return df.format(answerAsDecimal);
        } catch (NumberFormatException e) {
            return fep.getAnswerText();
        }
    }
    if (data != null && data.getValue() != null && fep.getDataType() == DATATYPE_TEXT && fep.getQuestion().getAdditionalAttribute(null, "query") != null) {
        // ItemsetWidget
        return new ItemsetDao().getItemLabel(fep.getAnswerValue().getDisplayText(), formController.getMediaFolder().getAbsolutePath(), formController.getLanguage());
    }
    return fep.getAnswerText();
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) DecimalFormatSymbols(java.text.DecimalFormatSymbols) Selection(org.javarosa.core.model.data.helper.Selection) DecimalFormat(java.text.DecimalFormat) BigDecimal(java.math.BigDecimal) ItemsetDao(org.odk.collect.android.dao.ItemsetDao) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) DateData(org.javarosa.core.model.data.DateData) List(java.util.List) DateTimeData(org.javarosa.core.model.data.DateTimeData)

Aggregations

BigDecimal (java.math.BigDecimal)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 List (java.util.List)1 DateData (org.javarosa.core.model.data.DateData)1 DateTimeData (org.javarosa.core.model.data.DateTimeData)1 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)1 Selection (org.javarosa.core.model.data.helper.Selection)1 ItemsetDao (org.odk.collect.android.dao.ItemsetDao)1