从RecyclerView中的RadioGroup获取以前的RadioButton值

时间:2017-07-06 15:06:21

标签: java android android-layout android-recyclerview android-view

Recyclerview screenshot

在我检查选项“salt”时的上述值中,它给出了上面项目中相同位置的值。所以在这种情况下,它给了我“CH6”

当我检查“水”时,它会给我“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;
                }
            }

        }

提前致谢!

1 个答案:

答案 0 :(得分:0)

而不是:

RadioButton checked = (RadioButton)findViewById(checkedId);

执行:

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