我正在为用户填写调查表,其中一项要求是实施一个在用户回答问题时填满的进度条。
我无法理解的错误是列表框。如果我选择一个值,计数器就会很好,但是如果我选择另一个值,计数器会再次增加,当它应该保持相同的值时。
以下基本示例:
完成百分比:0%(默认下拉列表,其中“选择值”为默认值)
完成百分比:2%
完成百分比:4%(应该是2%,因为问题已经得到解答)
这是更改处理程序的代码:
final ValueChangeHandler<Boolean> booleanChangeHandler = new ValueChangeHandler<Boolean>()
{
@Override
public void onValueChange(final ValueChangeEvent<Boolean> event)
{
Boolean value = event.getValue();
changeAnsweredQuestionTotal(String.valueOf(value));
}
};
这会改变进度条。我还用它来检查文本框是否已填写。
public void changeAnsweredQuestionTotal(String value)
{
//Window.alert(value);
boolean isEmpty = (value == null) || value.equalsIgnoreCase("Null") ? true : value.isEmpty();
Integer answered;
Integer totalQuestions;
String text = this.getQuestionsAnswered().getText().substring(0, 2);
text= text.replaceAll("\\s+", "");
answered = text.isEmpty() ? 0 : Integer.valueOf(text);
answered = (answered < 0) ? 0 : answered;
totalQuestions = this.getTotalQuestions().getValue().isEmpty() ? 0 : Integer.valueOf(this.getTotalQuestions().getValue());
String result;
if (isEmpty)
{
answered = answered - 1;
answered = (answered < 0) ? 0 : answered;
result = answered + " questions answered. ";
}
else
{
if (!value.equalsIgnoreCase("null"))
{
answered = answered > totalQuestions ? totalQuestions : answered + 1;
}
result = answered + " questions answered. ";
}
Integer percent = (answered*100)/totalQuestions;
percent = (percent > 100) ? 100 : percent;
}
非常感谢任何帮助。
答案 0 :(得分:1)
首先,我认为您的方法“changeAnsweredQuestionTotal”过于复杂,如果您决定将来再使用它,则需要重构。
下面我将描述如何解决它。我也确定还有其他方法。
这个想法是为答案设置一个计数器(在Map<String,Integer>
的情况下)。关键是控件名称(例如问题1或问题2 ......)值可以是0或1,具体取决于是否应答。
每个列表小部件都应该有一个自定义ValueChangeHandler
。该处理程序将知道“源”(将用作计数器中的键),并将知道根据值更新计数器。
最后,您创建一个处理程序,一个实例,并分配给所有列表小部件以更新进度条。
以下是自定义处理程序的代码:
public class QuestionChangeHandler<T> implements ValueChangeHandler<T>{
private String source;
private Map<String, Integer> counter;
public QuestionChangeHandler(String source, Map<String, Integer> counter) {
super();
this.source = source;
this.counter = counter;
}
@Override
public void onValueChange(ValueChangeEvent<T> event) {
T value = event.getValue();
if (value == null) {
counter.put(source, Integer.valueOf(0));
} else {
counter.put(source, Integer.valueOf(1));
}
}
/**
* @return the source
*/
public String getSource() {
return source;
}
}
这是一个包含两个列表的小型工作应用程序:
public class AnswersApp implements EntryPoint {
private VerticalPanel mainPanel = new VerticalPanel();
private Label answersLabel = new Label("Answers");
private Label answerNumber = new Label("0");
private ValueListBox<Boolean> question1 = new ValueListBox<Boolean>(new BooleanRenderer(), new BooleanKeyProvider());
private ValueListBox<Boolean> question2 = new ValueListBox<Boolean>(new BooleanRenderer(), new BooleanKeyProvider());
private ValueChangeHandler<Boolean> updateHandler;
private HashMap<String, Integer> counter = new HashMap<String, Integer>();
public void onModuleLoad() {
initUpdateHandler();
List<Boolean> values = new ArrayList<Boolean>();
values.add(Boolean.TRUE);
values.add(Boolean.FALSE);
question1.setAcceptableValues(values);
question2.setAcceptableValues(values);
question1.addValueChangeHandler(new QuestionChangeHandler<Boolean>("q1", counter));
question2.addValueChangeHandler(new QuestionChangeHandler<Boolean>("q2", counter));
question1.addValueChangeHandler(updateHandler);
question2.addValueChangeHandler(updateHandler);
mainPanel.add(question1);
mainPanel.add(question2);
mainPanel.add(answersLabel);
mainPanel.add(answerNumber);
RootPanel.get().add(mainPanel);
}
private void initUpdateHandler() {
updateHandler = new ValueChangeHandler<Boolean>() {
@Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
recalculateAnswers();
}
};
}
private void recalculateAnswers() {
Set<String> keys = counter.keySet();
int answers = 0;
for (String key : keys) {
Integer value = counter.get(key);
if (value != null && value.intValue() == 1) {
answers++;
}
}
answerNumber.setText(Integer.toString(answers));
}
}
结果是答案的数量,我相信你可以从那里继续。