Radio Button Cardview(List/Recyclerview)

Asked

Viewed 226 times

0

I have a Recycler View with Cardview questions. Each question has 3 radio button(yes, maybe, no) as the answer. Until then everything was working, except that;

When we answer the questions. The radioButtons of the following questions mark themselves. And if we change the answer and return to the list change alone the answers of the previous questions.

Thanks help... follow code.

inserir a descrição da imagem aqui

   public class QuizRecyclerViewAdapter extends RecyclerView.Adapter<QuizRecyclerViewAdapter.QuizViewHolder> {

    Context context;
    //
    private List<PerguntasQuiz> perguntasQuizList;

    //
    public QuizRecyclerViewAdapter() {
        this.perguntasQuizList = new PerguntasDAO().listaTodasPerguntasBanco();
    }

    //
    public QuizRecyclerViewAdapter(List<PerguntasQuiz> perguntasQuizList) {
        this.perguntasQuizList = perguntasQuizList;
    }

    @Override
    public QuizViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Desta forma o card view não pega o match_Parent no layout_width
        //View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.perguntas_card_view, null);

        // Assim(false) a view raiz, é usada somente para inflar o outro xml(card_view)
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.perguntas_card_view, parent, false);
        QuizViewHolder quizViewHolder = new QuizViewHolder(view);
        return quizViewHolder;

    }

    @Override
    public void onBindViewHolder(final QuizViewHolder holder, final int position) {
        // Recupera as informações na posição em questão.
        final PerguntasQuiz perguntasQuiz = perguntasQuizList.get(position);
        //


        // Modifica o texto na View.
        holder.textViewPerguntas.setText(perguntasQuiz.getPergunta());
        //
        holder.radioButtonSim.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                atualizaListaPerguntaQuizResposta(holder.getLayoutPosition(), PerguntasQuiz.RESPOSTA_SIM);

            }
        });

        holder.radioButtonAsVezes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                atualizaListaPerguntaQuizResposta(holder.getLayoutPosition(), PerguntasQuiz.RESPOSTA_AS_VEZES);

            }
        });

        holder.radioButtonNao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                atualizaListaPerguntaQuizResposta(holder.getLayoutPosition(), PerguntasQuiz.RESPOSTA_NAO);
            }
      });
    }

    public void atualizaListaPerguntaQuizResposta(int posição, int resposta) {

        perguntasQuizList

    .get(posição).setResposta(resposta);

        }
    public List<PerguntasQuiz> getPerguntasQuizList() {

        return perguntasQuizList;

    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public long getItemId(int position) {
        PerguntasQuiz perguntasQuiz = perguntasQuizList.get(position);
        long reposta;

        return super.getItemId(position);
    }

    @Override
    public boolean equals(Object o) {
        return super.equals(o);
    }

    @Override
    public int getItemCount() {
        int tamanhoLista;
        if (perguntasQuizList == null || perguntasQuizList.isEmpty()) {
            tamanhoLista = 0;
        } else {
            tamanhoLista = this.perguntasQuizList.size();
        }

        return tamanhoLista;
    }

    public static class QuizViewHolder extends RecyclerView.ViewHolder {

        private TextView textViewPerguntas;
        private RadioGroup radioGroup;
        private RadioButton radioButtonSim;
        private RadioButton radioButtonAsVezes;
        private RadioButton radioButtonNao;


        public QuizViewHolder(View itemView) {
            super(itemView);
            radioGroup = (RadioGroup) itemView.findViewById(R.id.card_view_radio_group);
            radioButtonSim = (RadioButton) itemView.findViewById(R.id.card_view_radio_button_sim);
            radioButtonAsVezes = (RadioButton) itemView.findViewById(R.id.card_view_radio_button_as_vezes);
            radioButtonNao = (RadioButton) itemView.findViewById(R.id.card_view_radio_button_nao);
            textViewPerguntas = (TextView) itemView.findViewById(R.id.text_view_id_perguntas_quiz);
            //
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if (checkedId == R.id.card_view_radio_button_sim) {
                        radioButtonSim.setChecked(true);
                        group.setTag(checkedId);
                        Log.i("log", "Item: " + getAdapterPosition() + " selecionado SIM");
                    } else if (checkedId == R.id.card_view_radio_button_as_vezes) {
                        radioButtonAsVezes.setChecked(true);
                        group.setTag(checkedId);
                        Log.i("log", "Item: " + getAdapterPosition() + " selecionado AS VEZES");
                    } else if (checkedId == R.id.card_view_radio_button_nao) {
                        radioButtonNao.setChecked(true);
                                  group.setTag(checkedId);
                        Log.i("log", "Item: " + getAdapterPosition() + " selecionado NÃO");
                    } else {
                        group.clearCheck();
                    }

                }
            });

        }
        }
    }




    public class QuizEmocionalActivity extends FragmentActivity {

    private AdView adView;
    private RecyclerView recyclerView;
    QuizRecyclerViewAdapter quizRecyclerViewAdapter;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz_emocional);

        //

        // Admob
        adView = (AdView) findViewById(R.id.quiz_activity_admob);
        adView.setAdListener(new ToastListener(this));
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
        //
        recyclerView = (RecyclerView) findViewById(R.id.quiz_emocional_recycle_view);
        recyclerView.setHasFixedSize(true);
        //
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        //

        //  List<PerguntasQuiz> perguntasQuizList =  perguntasDAO.listaTodasPerguntasBanco();

//        Informacao informacao = informacaoDAO.recuoearInfoPeloId(2);
//        Toast.makeText(this, informacao.getTitulo(), Toast.LENGTH_LONG).show()

//QuizRecyclerViewAdapter quizRecyclerViewAdapter = new QuizRecyclerViewAdapter();
        this.quizRecyclerViewAdapter = new QuizRecyclerViewAdapter();
        recyclerView.setAdapter(quizRecyclerViewAdapter);

    }


    public void onClickSalvarQuiz(View view) {

        List<PerguntasQuiz> perguntasQuizList = quizRecyclerViewAdapter.getPerguntasQuizList();

        int pontuacao = 0;
        boolean ok = true;

        for (int i = 0; i < perguntasQuizList.size(); i++) {

            if (perguntasQuizList.get(i).getResposta() == -1) {

                alertRespostasQuiz(i + 1);
                ok = false;
                break;

            } else {
                pontuacao += perguntasQuizList.get(i).getResposta();

            }

        }

        if (ok == true) {
            // salva Quiz
            chamaSalvarQuiz(pontuacao);
            // passa e mostra resultado em ResultadoQuizActivity
            Bundle bundle = new Bundle();
            bundle.putString("pontuacao", String.valueOf(pontuacao));
            Intent intent = new Intent(this, ResultadoQuizActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
            this.finish();
        }
    }

    //
    public boolean chamaSalvarQuiz(int percResultado) {
        boolean ok = true;

        Quiz quiz = new Quiz();
        QuizDAO quizDAO = new QuizDAO();
        //
        quiz.setPercResultado(percResultado);
        quiz.setDataQuiz(new Date());
        //
        ok = quizDAO.salvarQuiz(quiz);
        //
        return ok;
    }

    //
    public void alertRespostasQuiz(int pontuacao) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(QuizEmocionalActivity.this);
        //
        String mensagem = getString(R.string.texto_item_nao_respondido_quiz_activity) + " \""
                + pontuacao + "\" " + getString(R.string.texto_nao_respondido_quiz_activity);
        String responder = this.getString(R.string.texto_alert_responder_quiz);
        //
        //  builder.setTitle(titulo);
        builder.setMessage(mensagem);
        //
//        builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                nop
//            }
//        });
        builder.setNeutralButton(responder, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        //
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }


    public void onClickZoomMenos(View view) {

        Button button = (Button) findViewById(R.id.activity_quiz_mocional_zoom_menos);
        button.setTextColor(Color.GRAY);

    }

    public void onClickZoomMais(View view) {
        RadioButton radioButton = (RadioButton) findViewById(R.id.card_view_radio_button_sim);
        if (radioButton.isChecked()) {
            Toast.makeText(this, "Zoom mais clicado", Toast.LENGTH_SHORT).show();
        }
    }

    // Antigo, passando a usar método acima.
//    /**
//     * The pager widget, which handles animation and allows swiping horizontally to access previous
//     * and next wizard steps.
//     */
//    private ViewPager mPager;
//    /**
//     * The pager adapter, which provides the pages to the view pager widget.
//     */
//    private PagerAdapter mPagerAdapter;
//    private String[] textos = {"Questao 1", "Questao 2", "Questao 3", "Questao 4", "Questao 5"};
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_quiz_emocional);
//        //
//        // Instantiate a ViewPager and a PagerAdapter.
//        mPager = (ViewPager) findViewById(R.id.quiz_view_pager);
//        mPagerAdapter = new TelaSlidePagerAdapter(getSupportFragmentManager());
//        mPager.setAdapter(mPagerAdapter);
//
//    }


    //

}
  • Some help, if you please...

  • Any help, please... -

No answers

Browser other questions tagged

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