Search in sources :

Example 16 with SelectChoice

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

the class ExternalDataHandlerSearch method createDynamicSelectChoices.

protected ArrayList<SelectChoice> createDynamicSelectChoices(Cursor c, LinkedHashMap<String, String> selectColumnMap, String safeImageColumn) {
    List<String> columnsToExcludeFromLabels = new ArrayList<String>();
    if (safeImageColumn != null) {
        columnsToExcludeFromLabels.add(safeImageColumn);
    }
    ArrayList<SelectChoice> selectChoices = new ArrayList<SelectChoice>();
    if (c.getCount() > 0) {
        c.moveToPosition(-1);
        int index = 0;
        Set<String> uniqueValues = new HashSet<String>();
        while (c.moveToNext()) {
            // the value is always the first column
            String value = c.getString(0);
            if (!uniqueValues.contains(value)) {
                String label = buildLabel(c, selectColumnMap, columnsToExcludeFromLabels);
                ExternalSelectChoice selectChoice;
                if (label.trim().length() == 0) {
                    selectChoice = new ExternalSelectChoice(value, value, false);
                } else {
                    selectChoice = new ExternalSelectChoice(label, value, false);
                }
                selectChoice.setIndex(index);
                if (safeImageColumn != null && safeImageColumn.trim().length() > 0) {
                    String image = c.getString(c.getColumnIndex(safeImageColumn));
                    if (image != null && image.trim().length() > 0) {
                        selectChoice.setImage(ExternalDataUtil.JR_IMAGES_PREFIX + image);
                    }
                }
                selectChoices.add(selectChoice);
                index++;
                uniqueValues.add(value);
            }
        }
    }
    return selectChoices;
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) ExternalSelectChoice(org.odk.collect.android.external.ExternalSelectChoice) ArrayList(java.util.ArrayList) ExternalSelectChoice(org.odk.collect.android.external.ExternalSelectChoice) HashSet(java.util.HashSet)

Example 17 with SelectChoice

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

the class GridMultiWidgetTest method getAnswerShouldReflectWhichSelectionsWereMade.

@Test
public void getAnswerShouldReflectWhichSelectionsWereMade() {
    GridMultiWidget widget = getWidget();
    assertNull(widget.getAnswer());
    List<SelectChoice> selectChoices = getSelectChoices();
    List<String> selectedValues = new ArrayList<>();
    boolean atLeastOneSelected = false;
    for (int i = 0; i < widget.selected.length; i++) {
        boolean shouldBeSelected = random.nextBoolean();
        widget.selected[i] = shouldBeSelected;
        atLeastOneSelected = atLeastOneSelected || shouldBeSelected;
        if (shouldBeSelected) {
            SelectChoice selectChoice = selectChoices.get(i);
            selectedValues.add(selectChoice.getValue());
        }
    }
    // null answer case:
    if (!atLeastOneSelected) {
        int randomIndex = (Math.abs(random.nextInt()) % widget.selected.length);
        widget.selected[randomIndex] = true;
        SelectChoice selectChoice = selectChoices.get(randomIndex);
        selectedValues.add(selectChoice.getValue());
    }
    SelectMultiData answer = (SelectMultiData) widget.getAnswer();
    @SuppressWarnings("unchecked") List<Selection> answerSelections = (List<Selection>) answer.getValue();
    List<String> answerValues = selectionsToValues(answerSelections);
    for (String selectedValue : selectedValues) {
        assertTrue(answerValues.contains(selectedValue));
    }
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) List(java.util.List) ArrayList(java.util.ArrayList) GeneralSelectMultiWidgetTest(org.odk.collect.android.widgets.base.GeneralSelectMultiWidgetTest) Test(org.junit.Test)

Example 18 with SelectChoice

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

the class GeneralSelectMultiWidgetTest method getAnswerShouldReflectWhichSelectionsWereMade.

@Test
public void getAnswerShouldReflectWhichSelectionsWereMade() {
    W widget = getWidget();
    assertNull(widget.getAnswer());
    List<SelectChoice> selectChoices = getSelectChoices();
    List<String> selectedValues = new ArrayList<>();
    boolean atLeastOneSelected = false;
    for (int i = 0; i < widget.getChoiceCount(); i++) {
        boolean shouldBeSelected = random.nextBoolean();
        widget.setChoiceSelected(i, shouldBeSelected);
        atLeastOneSelected = atLeastOneSelected || shouldBeSelected;
        if (shouldBeSelected) {
            SelectChoice selectChoice = selectChoices.get(i);
            selectedValues.add(selectChoice.getValue());
        }
    }
    // null answer case:
    if (!atLeastOneSelected) {
        int randomIndex = (Math.abs(random.nextInt()) % widget.getChoiceCount());
        widget.setChoiceSelected(randomIndex, true);
        SelectChoice selectChoice = selectChoices.get(randomIndex);
        selectedValues.add(selectChoice.getValue());
    }
    SelectMultiData answer = (SelectMultiData) widget.getAnswer();
    @SuppressWarnings("unchecked") List<Selection> answerSelections = (List<Selection>) answer.getValue();
    List<String> answerValues = selectionsToValues(answerSelections);
    for (String selectedValue : selectedValues) {
        assertTrue(answerValues.contains(selectedValue));
    }
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 19 with SelectChoice

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

the class GeneralSelectOneWidgetTest method getNextAnswer.

@NonNull
@Override
public SelectOneData getNextAnswer() {
    List<SelectChoice> selectChoices = getSelectChoices();
    int selectedIndex = Math.abs(random.nextInt()) % selectChoices.size();
    SelectChoice selectChoice = selectChoices.get(selectedIndex);
    Selection selection = new Selection(selectChoice);
    return new SelectOneData(selection);
}
Also used : SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) NonNull(android.support.annotation.NonNull)

Example 20 with SelectChoice

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

the class GeneralSelectOneWidgetTest method getAnswerShouldReflectTheCurrentlySelectedChoice.

@Test
public void getAnswerShouldReflectTheCurrentlySelectedChoice() {
    W widget = getWidget();
    assertNull(widget.getAnswer());
    List<SelectChoice> selectChoices = getSelectChoices();
    for (int i = 0; i < widget.getChoiceCount(); i++) {
        widget.setChoiceSelected(i, true);
        SelectChoice selectChoice = selectChoices.get(i);
        IAnswerData answer = widget.getAnswer();
        assertEquals(selectChoice.getValue(), answer.getDisplayText());
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) SelectChoice(org.javarosa.core.model.SelectChoice) Test(org.junit.Test)

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