2012-10-31 12 views
6

私はラジオボタンの選択とラベルの投票で投票ウィジェットを持ってGWTのRadioButtonの変更ハンドラ

  1. ユーザーが選択肢を選択すると、選択票が+1べきです。
  2. 別の選択肢を選択すると、古い選択票は-1になり、新しい選択票は+1になります。ユーザーが新しい選択肢を選択すると

    valueRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() { 
          @Override 
          public void onValueChange(ValueChangeEvent<Boolean> e) { 
           if(e.getValue() == true) 
           { 
            System.out.println("select"); 
            votesPlusDelta(votesLabel, +1); 
           } 
           else 
           { 
            System.out.println("deselect"); 
            votesPlusDelta(votesLabel, -1); 
           } 
          } 
         }); 
    
    private void votesPlusDelta(Label votesLabel, int delta) 
    { 
        int votes = Integer.parseInt(votesLabel.getText()); 
        votes = votes + delta; 
        votesLabel.setText(votes+""); 
    } 
    

    、古い選択肢のリスナーはelseステートメントにジャンプする必要がありますが、それはしません(のみ+1一部の作品):

私はこのためにValueChangeHandlerを使用しました。私は何をすべきか?

答えて

9

を提案していますValueChangeEventラジオボタンがクリアされたときのイベントです。残念ながら、これはすべての簿記を自分で行う必要があることを意味します。

List<RadioButton> allRadioButtons = new ArrayList<RadioButton>(); 

// Add all radio buttons to list here 

for (RadioButton radioButton : allRadioButtons) { 
    radioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() { 
      @Override 
      public void onValueChange(ValueChangeEvent<Boolean> e) { 
       updateVotes(allRadioButtons.indexOf(radioButton)); 
     }); 
} 
:あなたはラジオボタンを初期化するときに

private int lastChoice = -1; 
private Map<Integer, Integer> votes = new HashMap<Integer, Integer>(); 
// Make sure to initialize the map with whatever you need 

:GWTの課題追跡に示唆されているように、あなた自身は、RadioButtonGroupクラスを作成するための代替として

は、あなたがこのような何かをやって検討することもでき

updateVotesメソッドは次のようになります。

private void updateVotes(int choice) { 
    if (votes.containsKey(lastChoice)) { 
     votes.put(lastChoice, votes.get(lastChoice) - 1); 
    } 

    votes.put(choice, votes.get(choice) + 1); 
    lastChoice = choice; 

    // Update labels using the votes map here 
} 

それほど優雅ではありませんが、それは仕事をする必要があります。

+0

ありがとう、私はそれが動作すると思います! ;) – united

2

GWT issue trackerには、この特定の問題に未解決の問題があります。最後のコメントは、あなたが受信しないことをRadioButton javadocに述べている...基本的に、あなたがすべてのラジオボタンにchangehandlersを持っており、グループ化、自分を追跡する必要が表示され、

乾杯、

+1

問題はgithubに移されました:https://github.com/gwtproject/gwt/issues/3467 –

関連する問題