Search in sources :

Example 21 with SelectChoice

use of org.javarosa.core.model.SelectChoice in project javarosa by opendatakit.

the class XFormAnswerDataParser method getSelection.

private static Selection getSelection(String choiceValue, QuestionDef q) {
    Selection s;
    if (q == null || q.getDynamicChoices() != null) {
        s = new Selection(choiceValue);
    } else {
        SelectChoice choice = q.getChoiceForValue(choiceValue);
        s = (choice != null ? choice.selection() : null);
    }
    return s;
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection)

Example 22 with SelectChoice

use of org.javarosa.core.model.SelectChoice in project javarosa by opendatakit.

the class FormEntryPrompt method getAnswerValue.

// note: code overlap with FormDef.copyItemsetAnswer
public IAnswerData getAnswerValue() {
    QuestionDef q = getQuestion();
    ItemsetBinding itemset = q.getDynamicChoices();
    if (itemset != null) {
        if (itemset.valueRef != null) {
            List<SelectChoice> choices = getSelectChoices();
            List<String> preselectedValues = new ArrayList<String>();
            // determine which selections are already present in the answer
            if (itemset.copyMode) {
                TreeReference destRef = itemset.getDestRef().contextualize(mTreeElement.getRef());
                List<TreeReference> subNodes = form.getEvaluationContext().expandReference(destRef);
                for (int i = 0; i < subNodes.size(); i++) {
                    TreeElement node = form.getMainInstance().resolveReference(subNodes.get(i));
                    String value = itemset.getRelativeValue().evalReadable(form.getMainInstance(), new EvaluationContext(form.getEvaluationContext(), node.getRef()));
                    preselectedValues.add(value);
                }
            } else {
                List<Selection> sels;
                IAnswerData data = mTreeElement.getValue();
                if (data instanceof SelectMultiData) {
                    sels = (List<Selection>) data.getValue();
                } else if (data instanceof SelectOneData) {
                    sels = new ArrayList<Selection>(1);
                    sels.add((Selection) data.getValue());
                } else {
                    sels = new ArrayList<Selection>(0);
                }
                for (int i = 0; i < sels.size(); i++) {
                    preselectedValues.add(sels.get(i).xmlValue);
                }
            }
            // populate 'selection' with the corresponding choices (matching 'value') from the dynamic choiceset
            List<Selection> selection = new ArrayList<Selection>();
            for (int i = 0; i < preselectedValues.size(); i++) {
                String value = preselectedValues.get(i);
                SelectChoice choice = null;
                for (int j = 0; j < choices.size(); j++) {
                    SelectChoice ch = choices.get(j);
                    if (value.equals(ch.getValue())) {
                        choice = ch;
                        break;
                    }
                }
                // will no longer be an option this go around
                if (choice != null) {
                    selection.add(choice.selection());
                }
            }
            // convert to IAnswerData
            if (selection.size() == 0) {
                return null;
            } else if (q.getControlType() == Constants.CONTROL_SELECT_MULTI) {
                return new SelectMultiData(selection);
            } else if (q.getControlType() == Constants.CONTROL_SELECT_ONE) {
                // do something if more than one selected?
                return new SelectOneData(selection.get(0));
            } else {
                throw new RuntimeException("can't happen");
            }
        } else {
            // cannot map up selections without <value>
            return null;
        }
    } else {
        // static choices
        return mTreeElement.getValue();
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList) ItemsetBinding(org.javarosa.core.model.ItemsetBinding) ConstraintHint(org.javarosa.core.model.condition.pivot.ConstraintHint) Constraint(org.javarosa.core.model.condition.Constraint) TreeElement(org.javarosa.core.model.instance.TreeElement) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) TreeReference(org.javarosa.core.model.instance.TreeReference) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 23 with SelectChoice

use of org.javarosa.core.model.SelectChoice in project javarosa by opendatakit.

the class SelectMultiDataTests method setUp.

/* (non-Javadoc)
     * @see j2meunit.framework.TestCase#setUp()
     */
public void setUp() throws Exception {
    super.setUp();
    question = new QuestionDef();
    for (int i = 0; i < 4; i++) {
        question.addSelectChoice(new SelectChoice("", "Selection" + i, "Selection " + i, false));
    }
    one = new Selection("Selection 1");
    one.attachChoice(question);
    two = new Selection("Selection 2");
    two.attachChoice(question);
    three = new Selection("Selection 3");
    three.attachChoice(question);
    firstTwo = new ArrayList<Selection>();
    firstTwo.add(one);
    firstTwo.add(two);
    lastTwo = new ArrayList<Selection>();
    lastTwo.add(two);
    lastTwo.add(three);
    invalid = new ArrayList<Object>();
    invalid.add(three);
    invalid.add(new Integer(12));
    invalid.add(one);
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 24 with SelectChoice

use of org.javarosa.core.model.SelectChoice in project javarosa by opendatakit.

the class FormParseInit method printStuff.

/*
     * Makes an 'extremely basic' print out of the xform model.
     */
public String printStuff() {
    String stuff = "";
    // go to the beginning of the form
    fec.jumpToIndex(FormIndex.createBeginningOfFormIndex());
    do {
        FormEntryCaption fep = femodel.getCaptionPrompt();
        boolean choiceFlag = false;
        if (fep.getFormElement() instanceof QuestionDef) {
            stuff += "\t[Type:QuestionDef, ";
            List<SelectChoice> s = ((QuestionDef) fep.getFormElement()).getChoices();
            stuff += "ContainsChoices: " + ((s != null && s.size() > 0) ? "true " : "false") + ", ";
            if (s != null && s.size() > 0)
                choiceFlag = true;
        } else if (fep.getFormElement() instanceof FormDef) {
            stuff += "\t[Type:FormDef, ";
        } else if (fep.getFormElement() instanceof GroupDef) {
            stuff += "\t[Type:GroupDef, ";
        } else {
            stuff += "\t[Type:Unknown]\n";
            continue;
        }
        stuff += "ID:" + fep.getFormElement().getID() + ", TextID:" + fep.getFormElement().getTextID() + ",InnerText:" + fep.getFormElement().getLabelInnerText();
        if (choiceFlag) {
            stuff += "] \n\t\t---Choices:" + ((QuestionDef) fep.getFormElement()).getChoices().toString() + "\n";
        } else {
            stuff += "]\n";
        }
    } while (fec.stepToNextEvent() != fec.EVENT_END_OF_FORM);
    return stuff;
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) FormDef(org.javarosa.core.model.FormDef) FormEntryCaption(org.javarosa.form.api.FormEntryCaption) QuestionDef(org.javarosa.core.model.QuestionDef) GroupDef(org.javarosa.core.model.GroupDef)

Aggregations

SelectChoice (org.javarosa.core.model.SelectChoice)24 ArrayList (java.util.ArrayList)12 Selection (org.javarosa.core.model.data.helper.Selection)12 QuestionDef (org.javarosa.core.model.QuestionDef)7 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)7 SuppressLint (android.annotation.SuppressLint)5 SelectOneData (org.javarosa.core.model.data.SelectOneData)4 List (java.util.List)3 Test (org.junit.Test)3 NonNull (android.support.annotation.NonNull)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 IAnswerData (org.javarosa.core.model.data.IAnswerData)2 TreeElement (org.javarosa.core.model.instance.TreeElement)2 ExternalSelectChoice (org.odk.collect.android.external.ExternalSelectChoice)2 Cursor (android.database.Cursor)1 SQLiteException (android.database.sqlite.SQLiteException)1 WebView (android.webkit.WebView)1 WebViewClient (android.webkit.WebViewClient)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1