Back to project page sana.
The source code is released under:
Copyright (c) 2010, Moca All rights reserved. The source code for Moca is licensed under the BSD license as follows: Redistribution and use in source and binary forms, with or without modification, ...
If you think the Android project sana listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.moca.procedure; //w ww . java 2s . c om import java.util.ArrayList; import java.util.HashSet; import java.util.List; import org.moca.util.MocaUtil; import org.w3c.dom.Node; import android.content.Context; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.ScrollView; /** * MultiSelectElement is a ProcedureElement that creates a procedure page with * multiple check-box items that can be selected. */ public class MultiSelectElement extends ProcedureElement { private List<String> choicelist; private String[] choices; private ArrayList<CheckBox> cblist; public static final String TOKEN_DELIMITER = ","; @Override public ElementType getType() { return ElementType.MULTI_SELECT; } @Override protected View createView(Context c) { ScrollView sv = new ScrollView(c); LinearLayout ll = new LinearLayout(c); HashSet<String> selectedSet = new HashSet<String>(); if(answer == null) answer = ""; // we've got a problem if there are TOKEN_DELIMITERs in the value! // since getAnswer separates responses using TOKEN_DELIMITER String[] values = answer.split(TOKEN_DELIMITER); for(String val : values) { selectedSet.add(val); } ll.setOrientation(LinearLayout.VERTICAL); choicelist = java.util.Arrays.asList(choices); cblist = new ArrayList<CheckBox>(); for(Object choice : choicelist) { CheckBox cb = new CheckBox(c); cb.setText((String)choice); cb.setChecked(selectedSet.contains(choice)); cblist.add(cb); ll.addView(cb); } sv.addView(ll, new ViewGroup.LayoutParams(-1,-1)); return encapsulateQuestion(c, sv); } public void setAnswer(String answer) { this.answer = answer; if(isViewActive()) { String[] answers = answer.split(TOKEN_DELIMITER); HashSet<String> answerSet = new HashSet<String>(); for(String a : answers) { answerSet.add(a); Log.i(TAG, "SetAnswer a:" + a + ":"); } for (CheckBox c : cblist) { Log.i(TAG, "SetAnswer - :" + c.getText().toString() + ":"); if(answerSet.contains(c.getText().toString())) { c.setChecked(true); } else{ c.setChecked(false); } } } } /** * Generate a string representing all of the user-selected elements delimited by * TOKEN_DELIMITER. */ public String getAnswer() { if(!isViewActive()) return answer; String s = ""; boolean any = false; for (CheckBox c : cblist) { if (c.isChecked()) { s += c.getText().toString() + TOKEN_DELIMITER; any = true; } } if(any) s = s.substring(0, s.length()-1); return s; } /** * Make question and response into an XML string for storing or transmission. */ public void buildXML(StringBuilder sb) { sb.append("<Element type=\"" + getType().name() + "\" id=\"" + id); sb.append("\" question=\"" + question); sb.append("\" choices=\"" + TextUtils.join(TOKEN_DELIMITER, choices)); sb.append("\" answer=\"" + getAnswer()); sb.append("\" concept=\"" + getConcept()); sb.append("\"/>\n"); } private MultiSelectElement(String id, String question, String answer, String concept, String figure, String audio, String[] choices) { super(id, question, answer, concept, figure, audio); this.choices = choices; } /** * Create a MultiSelectElement from an XML procedure definition. */ public static MultiSelectElement fromXML(String id, String question, String answer, String concept, String figure, String audio, Node node) { String choicesStr = MocaUtil.getNodeAttributeOrDefault(node, "choices", ""); return new MultiSelectElement(id, question, answer, concept, figure, audio, choicesStr.split(",")); } }