Radio Button CardView (List / RecyclerView)

0

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

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

Thanks for your help ... follow code.

publicclassQuizRecyclerViewAdapterextendsRecyclerView.Adapter<QuizRecyclerViewAdapter.QuizViewHolder>{Contextcontext;//privateList<PerguntasQuiz>perguntasQuizList;//publicQuizRecyclerViewAdapter(){this.perguntasQuizList=newPerguntasDAO().listaTodasPerguntasBanco();}//publicQuizRecyclerViewAdapter(List<PerguntasQuiz>perguntasQuizList){this.perguntasQuizList=perguntasQuizList;}@OverridepublicQuizViewHolderonCreateViewHolder(ViewGroupparent,intviewType){//Destaformaocardviewnãopegaomatch_Parentnolayout_width//Viewview=LayoutInflater.from(parent.getContext()).inflate(R.layout.perguntas_card_view,null);//Assim(false)aviewraiz,éusadasomenteparainflarooutroxml(card_view)Viewview=LayoutInflater.from(parent.getContext()).inflate(R.layout.perguntas_card_view,parent,false);QuizViewHolderquizViewHolder=newQuizViewHolder(view);returnquizViewHolder;}@OverridepublicvoidonBindViewHolder(finalQuizViewHolderholder,finalintposition){//Recuperaasinformaçõesnaposiçãoemquestão.finalPerguntasQuizperguntasQuiz=perguntasQuizList.get(position);////ModificaotextonaView.holder.textViewPerguntas.setText(perguntasQuiz.getPergunta());//holder.radioButtonSim.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){atualizaListaPerguntaQuizResposta(holder.getLayoutPosition(),PerguntasQuiz.RESPOSTA_SIM);}});holder.radioButtonAsVezes.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){atualizaListaPerguntaQuizResposta(holder.getLayoutPosition(),PerguntasQuiz.RESPOSTA_AS_VEZES);}});holder.radioButtonNao.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){atualizaListaPerguntaQuizResposta(holder.getLayoutPosition(),PerguntasQuiz.RESPOSTA_NAO);}});}publicvoidatualizaListaPerguntaQuizResposta(intposição,intresposta){perguntasQuizList.get(posição).setResposta(resposta);}publicList<PerguntasQuiz>getPerguntasQuizList(){returnperguntasQuizList;}@OverridepublicintgetItemViewType(intposition){returnsuper.getItemViewType(position);}@OverridepubliclonggetItemId(intposition){PerguntasQuizperguntasQuiz=perguntasQuizList.get(position);longreposta;returnsuper.getItemId(position);}@Overridepublicbooleanequals(Objecto){returnsuper.equals(o);}@OverridepublicintgetItemCount(){inttamanhoLista;if(perguntasQuizList==null||perguntasQuizList.isEmpty()){tamanhoLista=0;}else{tamanhoLista=this.perguntasQuizList.size();}returntamanhoLista;}publicstaticclassQuizViewHolderextendsRecyclerView.ViewHolder{privateTextViewtextViewPerguntas;privateRadioGroupradioGroup;privateRadioButtonradioButtonSim;privateRadioButtonradioButtonAsVezes;privateRadioButtonradioButtonNao;publicQuizViewHolder(ViewitemView){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(newRadioGroup.OnCheckedChangeListener(){@OverridepublicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){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);
//
//    }


    //

}
    
asked by anonymous 30.09.2016 / 21:24

0 answers