我正在创建一个供用户填写的调查问卷,其中一项要求是实现一个进度条,该进度条将在用户回答问题时填满。

我无法完全解决的错误是列表框。如果我选择一个值,则计数器会上升,但是如果我选择另一个值,则该计数器应再次保持不变,然后再次增加。

下面的基本示例:


选择值

真正




完成百分比: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;
}


任何帮助将不胜感激。

最佳答案

首先,我认为您的方法“ 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));
  }
}


结果是答案的数量,我确定您可以从那里继续。

09-11 09:08