从RecyclerView中的RadioGroup获取以前的RadioButton值 [英] Getting previous RadioButton value from RadioGroup inside a RecyclerView

查看:353
本文介绍了从RecyclerView中的RadioGroup获取以前的RadioButton值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上面的值中,当我选中盐"选项时,它为我提供了与以上项目相同位置的值.所以在这种情况下,它给了我"CH6"

In the above value when I check the option "salt" it gives me the value of same position from the above item. So in this case it gives me "CH6"

就像这样,当我选中水"时,它会给我"CH4".

Like this when i check "Water" it gives me "CH4".

这是我的RecyclerView

  public class QuestionsAdapter extends 

    RecyclerView.Adapter<QuestionsAdapter.ViewHolder> {

            List<String> names = new ArrayList<>();
            Context c;
            String username;
            Map<String,Object> map = new HashMap<>();
            ArrayList<QuestionData> questions = new ArrayList<>();



            public QuestionsAdapter(Context c) {
                this.c = c;
            }

            public void updateQuestion(ArrayList<QuestionData> data){
                this.questions = data;
                notifyDataSetChanged();
            }
            public class ViewHolder extends RecyclerView.ViewHolder {
                TextView q;
                RadioButton r1;
                RadioButton r2;
                RadioButton r3;
                RadioButton r4;
                RadioGroup rg;

                public ViewHolder(View itemView) {
                    super (itemView);
                    rg = (RadioGroup)itemView.findViewById(R.id.RGroup);
                    q = (TextView)itemView.findViewById(R.id.question);
                    r1 = (RadioButton)itemView.findViewById(R.id.no1);
                    r2 = (RadioButton)itemView.findViewById(R.id.no2);
                    r3 = (RadioButton)itemView.findViewById(R.id.no3);
                    r4 = (RadioButton)itemView.findViewById(R.id.no4);


                }
            }


            @Override
            public QuestionsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.qnalayout, parent, false);
                return new ViewHolder(v);



            }

            @Override
            public void onBindViewHolder(final ViewHolder holder, int position) {
                final QuestionData qholder =  questions.get(position);

                String question= qholder.getQuestion();
                final String qid = qholder.getId();

                final ArrayList<String> options = new ArrayList<>();
                options.add(qholder.getO1());
                options.add(qholder.getO2());
                options.add(qholder.getO3());
                options.add(qholder.getO4());
                Collections.shuffle(options);
                final String o1 = options.get(0);
                String o2 = options.get(1);
                String o3 = options.get(2);
                String o4 =  options.get(3);
                final String[] answer = new String[1];
                holder.q.setText(question);
                holder.r1.setText(o1);
                holder.r2.setText(o2);
                holder.r3.setText(o3);
                holder.r4.setText(o4);
                holder.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                        switch (checkedId){
                            case R.id.no1:
                                Toast.makeText(c, holder.r1.getText().toString(), Toast.LENGTH_SHORT).show();
                                break;
                            case R.id.no2:
                                Toast.makeText(c, holder.r2.getText().toString(), Toast.LENGTH_SHORT).show();

                                break;
                            case R.id.no3:
                                Toast.makeText(c, holder.r3.getText().toString(), Toast.LENGTH_SHORT).show();

                                break;
                            case R.id.no4:
                                Toast.makeText(c, holder.r4.getText().toString(), Toast.LENGTH_SHORT).show();

                                break;
                        }
                        RadioButton checked = (RadioButton)findViewById(checkedId);
                        answer[0] = checked.getText().toString();
                        Toast.makeText(c, checked.getText().toString(), Toast.LENGTH_SHORT).show();
                        map.put(qid,answer[0]);
                        System.out.println(map);
                    }
                });

            }


            @Override
            public int getItemCount() {
                if (questions!=null){
                    return questions.size();

                }else {
                    return 0;
                }
            }

        }

提前谢谢!

推荐答案

而不是:

RadioButton checked = (RadioButton)findViewById(checkedId);

执行:

RadioButton checked = (RadioButton) group.findViewById(checkedId);

这篇关于从RecyclerView中的RadioGroup获取以前的RadioButton值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆