Mark / Uncheck RadioButton Component in ExpandedListView

2

In a question I made here in StackOverflow a few days ago I was instructed to use the ExpendedListView component to render a dynamic list of items.

After the implementation I saw that the items in the list were grouped correctly, but now I need to implement the return of the tags and I'm having a new problem.

What's happening is that my list loads perfectly, but I can not get RadioButtons to behave properly because they do not seem to be grouped, so it's possible to mark more than one item in the list. The code below shows how I'm doing to load list items into ExpandedListView and how I'm handling the click:

    final List<QuestoesRequest> questoes = questaoService.getListQuestoes(getIdAvaliacao());
    expListView = (ExpandableListView) findViewById(R.id.expandableListView);
    expListView.setAdapter(new QuestaoExpandedListAdapter(
            this, questoes));

    //Define um listener para tratar a selecção das respostas
    expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            boolean b = expListView.isItemChecked(childPosition);

            RadioButton answerText = (RadioButton) v.findViewById(R.id.answerText);
            answerText.setChecked(true);
            //Indica que esta resposta foi seleccionada
            int idQuestao = questoes.get(groupPosition).getIdQuestao();
            int idAlternativa = questoes.get(groupPosition)
                    .getAlternativasRequests().get(childPosition).idAlternativa;
            respostasQuestao.put(idQuestao, idAlternativa);
            return true;
        }
    });

Although I can visually display the RadioButton's markup, I can not undo it if I choose another option.

Any suggestions? Hugs!

    
asked by anonymous 03.12.2015 / 05:45

1 answer

0

To check the automatic uncheck when choosing another option, the RadioButton must be grouped within a RadioGroup .

In this case, I do not see how you can include the RadioButton in a RadioGroup .

The solution is to be the adapter having the responsibility of "checking" the RadioButton , in the getChildView() method, based on information stored in the model (1) .

No onChildClick() of OnChildClickListener indicates, in the question, which answer is chosen and make the refresh from the list with adapater.notifyDataSetChanged() .

See my answer for the question you mentioned to see how this implementation is done.

The responsibility of reflecting the status of the model in views should always be Adapter in> and not just in this case.

    
03.12.2015 / 11:41