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;
}
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));
}
}
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));
}
}
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);
}
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());
}
}
Aggregations