0

recently, I have written a scene to display the same as recyclerview in javafx using custom listview . My custom listcell

public class QuestionCell extends ListCell<TestGrammarModel.Question> {
    private HBox content;
    private HBox choose;
    private Text question;
    private RadioButton optionA;
    private RadioButton optionB;
    private RadioButton optionC;
    private RadioButton optionD;
    private Text ans;
    private Text key;
    private Text number;
    private VBox vBox;
    private ToggleGroup group;
    private boolean isSummitted;
    public QuestionCell(boolean isSummitted){
        super();
        question = new Text();
        optionA = new RadioButton();
        optionB = new RadioButton();
        optionC = new RadioButton();
        optionD = new RadioButton();

        number = new Text();
        content = new HBox(number, question);
        choose= new HBox(  optionA, optionB ,
                optionC ,optionD);
        HBox.setMargin(optionA, new Insets(60, 70, 30, 70));
        HBox.setMargin(optionB, new Insets(60, 70, 30, 70));
        HBox.setMargin(optionC, new Insets(60, 70, 30, 70));
        HBox.setMargin(optionD, new Insets(60, 70, 30, 70));
        group = new ToggleGroup();
        optionB.setToggleGroup(group);
        optionA.setToggleGroup(group);
        optionC.setToggleGroup(group);
        optionD.setToggleGroup(group);
        choose.setAlignment(Pos.CENTER);
        vBox = new VBox(content, choose);
        vBox.setSpacing(10);
        this.isSummitted = isSummitted;
    }

    public ToggleGroup getGroup() {
        return group;
    }

    @Override
    protected void updateItem(TestGrammarModel.Question item, boolean empty) {
        super.updateItem(item, empty);
        if (item !=null && !empty ){
            //set text
            question.setText(item.getQuestion());
            optionA.setText("A:  "+item.getOptionA());
            optionB.setText("B:  "+item.getOptionB());
            optionC.setText("C:  "+item.getOptionC());
            optionD.setText("D:  "+item.getOptionD());
            //set font
            optionA.setFont(Font.font(18));
            optionB.setFont(Font.font(18));
            optionC.setFont(Font.font(18));
            optionD.setFont(Font.font(18));
            question.setFont(Font.font(18));
            number.setFont(Font.font(18));
            //set user date
            optionA.setUserData(item.getOptionA());
            optionB.setUserData(item.getOptionB());
            optionC.setUserData(item.getOptionC());
            optionD.setUserData(item.getOptionD());
            number.setText(String.valueOf(item.getNumber()) + ": ");
            setGraphic(vBox);

          //when user did not click on Submit button 
        if (!isSummitted){
                group.selectedToggleProperty().addListener((obs, oldSel, newSel) -> {
                    if (group.getSelectedToggle() != null) {
                        if (group.getSelectedToggle().getUserData() != null ){

                            if (optionA.isSelected())
                                answer.get(selectedIdx)[getIndex()]  = "A";
                            else  if (optionB.isSelected())
                                answer.get(selectedIdx)[getIndex()]  = "B";
                            else  if (optionC.isSelected())
                                answer.get(selectedIdx)[getIndex()]  = "C";
                            else
                                answer.get(selectedIdx)[getIndex()]  = "D";
                            System.out.println(answer.get(selectedIdx)[getIndex()]);
                        }
                        System.out.println(group.getSelectedToggle().getUserData().toString());
                        //get topic's index is selected

                    }
                    if (answer.get(selectedIdx)[getIndex()].equals(item.getKey())){
                        answer.get(selectedIdx)[answer.get(selectedIdx).length -2] =
                                Integer.toString(Integer.parseInt(answer.get(selectedIdx)[answer.get(selectedIdx).length -2 ])+1);
                        answer.get(selectedIdx)[answer.get(selectedIdx).length -1] =
                                Integer.toString(Integer.parseInt(answer.get(selectedIdx)[answer.get(selectedIdx).length-1]) -1) ;
                    }
                });
            }
         }else { setGraphic(null);
                 }
              }

I setCellFactory here:

 listView.setCellFactory(param -> new QuestionCell(false));

When I clicked in the first cell or second cell in custom listview, the end cell of listview will be automatically selected when scrolling to end of listview. I try to debbug and figure out that whenever i scroll listview , some listcells are recreated and updated. I have search in stackoveflow and get it Weird recycle of cells in ListView - JavaFX but that cannot help me any. What thing is i do wrong? Here Is my problem. When I selected on question 1 was A and scrolled down , question 7 will be automatically selected A based on question 1 [https://s31.aconvert.com/convert/p3r68-cdx67/zj9aw-aiag5.gif][1]

0 Answers0