简体   繁体   中英

LinkedList and RadioButton

I have a LinkedList of all the selected answers of a user through radio buttons. I wish to set them as selected when revisited by picking up the earlier set answers in the LinkedList. How can this be achieved?

selected=(String) dtmarked.get(dtqno.indexOf(queno)-1);
System.out.println("SELECTED: "+selected);
if(selected.equals("choice1"))
    choice1.setSelected(true);
else if(selected.equals("choice2"))
    choice2.setSelected(true);
else if(selected.equals("choice3"))
    choice3.setSelected(true);
else if(selected.equals("choice4"))
    choice4.setSelected(true);

I tried following the above snipet but without success even though it prints the correct choice number. here dtmarked is my linkedlist

  public void choice()
    {

        if(choice1.isSelected())
        {   
            if(dtmarked.size()!=cnt)
                dtmarked.add("choice1");
            else
            {
                dtmarked.remove(dtqno.indexOf(queno));
                dtmarked.add(dtqno.indexOf(queno),"choice1");
            }
        }
        else if(choice2.isSelected())
        {   
            if(dtmarked.size()!=cnt)
                dtmarked.add("choice2");
                else
                {
                    dtmarked.remove(dtqno.indexOf(queno));
                    dtmarked.add(dtqno.indexOf(queno),"choice2");
                }
        }
        else if(choice3.isSelected())
        {
            if(dtmarked.size()!=cnt)
            dtmarked.add("choice3");
            else
            {
                dtmarked.remove(dtqno.indexOf(queno));
                dtmarked.add(dtqno.indexOf(queno),"choice3");
            }
        }
        else if(choice4.isSelected())
        {
            if(dtmarked.size()!=cnt)
            dtmarked.add("choice4");
            else
            {
                dtmarked.remove(dtqno.indexOf(queno));
                dtmarked.add(dtqno.indexOf(queno),"choice4");
            }
        }      
        else
        {           
            if(dtmarked.size()!=cnt)
            dtmarked.add("0");
            else
            {
                dtmarked.remove(dtqno.indexOf(queno));
                dtmarked.add(dtqno.indexOf(queno),"0");
            }
        }
        System.out.println(dtqno);
        System.out.println(dtmarked);
    }

This is what I have done to add the choices in the linked list. also here cnt is a variable returning number of entries in a table(in this case 5).

You shouldn't use else if because then only one of the if/else if blocks gets executed. You should only use if.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM