簡體   English   中英

從RecyclerView中的RadioGroup獲取以前的RadioButton值

[英]Getting previous RadioButton value from RadioGroup inside a RecyclerView

Recyclerview屏幕截圖

在上面的值中,當我選中“鹽”選項時,它為我提供了與以上項目相同位置的值。 所以在這種情況下,它給我“ 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;
                }
            }

        }

提前致謝!

代替:

RadioButton checked = (RadioButton)findViewById(checkedId);

執行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM