..Souce Code of org.moca.procedure.
package org.moca.procedure;
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
private String[] choices;
private ArrayList
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
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
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
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(“
}
private MultiSelectElement(String id, String question, String answer, String concept, String[] choices) {
super(id, question, answer, concept);
this.choices = choices;
}
/**
* Create a MultiSelectElement from an XML procedure definition.
*/
public static MultiSelectElement fromXML(String id, String question, String answer, String concept, Node node) {
String choicesStr = MocaUtil.getNodeAttributeOrDefault(node, “choices”, “”);
return new MultiSelectElement(id, question, answer, concept, choicesStr.split(“,”));
}
}
Related Class of org.moca.procedure.MultiSelectElement
Copyright © 2011 www.androidadb.com. All rights reserved. All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc. Contact . See also:
|
|
|