Mark/Deselect Radiobutton Component in Expandedlistview

Asked

Viewed 808 times

1

In a question I asked here at Stackoverflow a few days ago I was instructed to use the Expendedlistview component to represent a dynamic list of items.

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

What’s happening is that my list loads perfectly, but I can’t get the Radiobuttons to behave properly, because they don’t seem to be grouped together, so it’s being possible to mark more than one item on the list. The code below shows how I’m doing to load list items in Expandedlistview and how I’m handling 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 show visually the marking of Radiobutton I am not able to uncheck it, in case the choice of another option occurs.

Any suggestions? Hugs!

1 answer

0

To check the automatic unchecking when a choice of another option occurs, it is necessary that the Radiobutton are grouped within a Radiogroup.

In your case I don’t see how it’s possible to include Radiobutton in a Radiogroup.

The solution is to Adapter to have the responsibility to "check" the Radiobutton, in the method getChildView(), based on information stored in model(1).

In the onChildClick() of OnChildClickListener indicate, in the question, which is the chosen answer and do the refresh of the list through adapater.notifyDataSetChanged().

Consult my reply to the question you mentioned to see how this implementation is done.

(1)The responsibility to reflect the state of model in views should always be from Adapter and not just in this case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.