2012-04-03 66 views
0

我正在爲用戶填寫一份調查問卷,其中一項要求是實施用戶回答問題時填寫的進度欄。檢查ValueListBoxes是否在GWT中得到應答

我不能完全包裹我的頭的錯誤是與列表框。如果我選擇一個值,計數器上升很好,但如果我選擇另一個值,計數器會再次增加,當它保持相同的值時。

基本下面的例子:

  • 選擇值
    • TRUE
    • FALSE

完成百分比:0%(默認下拉列表與 「選擇值」作爲默認值)

  • TRUE
    • 選擇值
    • FALSE

完成百分比:2%

  • FALSE
    • 選擇值
    • TRUE

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

任何幫助將不勝感激。

回答

1

首先,我認爲你的方法「changeAnsweredQuestionTotal」太複雜,需要重構,如果你決定將來使用它。

在下面我將描述我將如何解決它。我確定還有其他方法。

這個想法是有一個計數器(在這種情況下爲Map<String,Integer>)的答案。關鍵是控制名稱(例如question1或question2 ...),值可以是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)); 
    } 
} 

這裏的結果是答案的數目,我相信你可以從那裏繼續。