【问题标题】:In a recyclerview how to determine the radio button clicked is from the same radio group in Android在回收站视图中,如何确定单击的单选按钮来自 Android 中的同一单选组
【发布时间】:2019-05-28 12:48:15
【问题描述】:

我有一个RadioGroup,在RecyclerView 中有4 个RadioButtons。 RecyclerView 适配器是这样的

    class QuestionRecyclerAdapter(var number:Int, val context: Context, val quesions: List<Question>,val qnADao: QnADao) : RecyclerView.Adapter<QuestionRecyclerAdapter.ViewHolder>() {
        lateinit var question:Question
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

            return ViewHolder(LayoutInflater.from(context).inflate(R.layout.question_single_item, parent, false));
        }

        override fun getItemCount(): Int {

            return quesions.size;
        }
         fun updateNumber(number: Int){
            this.number = number
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {

            question = quesions[position]
            holder.textTestQuestion.text = ""+(number+position)+" "+ question.question;
            holder.answerFirst.text = "a. "+ question.answers?.get(0).toString()
            holder.answerSecond.text = "b. "+ question.answers?.get(1).toString()
            holder.answerThird.text = "c. "+ question.answers?.get(2).toString()
            holder.answerFourth.text = "d. "+ question.answers?.get(3).toString()   

            holder.radio_group.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener{
                override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
                    when(checkedId){
                        R.id.answerFirst -> {
                            question.rightAnswer=0
                        }
                        R.id.answerSecond -> {
                            question.rightAnswer=1
                        }
                        R.id.answerThird -> {
                            question.rightAnswer=2
                        }
                        R.id.answerFourth -> {
                            question.rightAnswer=3
                        }
                    }

        }


        class ViewHolder(view: View) : RecyclerView.ViewHolder(view)  {
            val textTestQuestion = view.test_text_question
            val answerFirst = view.answerFirst
            val answerSecond = view.answerSecond
            val answerThird = view.answerThird
            val answerFourth = view.answerFourth
            val radio_group = view.radioGrp


        }
    }

question_single_item.xml是这样的

        <RadioButton
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:checked="false"
            android:drawableRight="@android:drawable/btn_radio"
            android:textSize="14dp" />

        <RadioButton
            android:id="@+id/second"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:checked="false"
            android:drawableRight="@android:drawable/btn_radio"
            android:textSize="14dp" />

        <RadioButton
            android:id="@+id/third"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:checked="false"
            android:drawableRight="@android:drawable/btn_radio"
            android:textSize="14dp" />

        <RadioButton
            android:id="@+id/fourth"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:checked="false"
            android:drawableRight="@android:drawable/btn_radio"
            android:textSize="14dp" />
    </RadioGroup>
</RelativeLayout>

在 recyclerview 中,我填充了近 100 条数据,因此有 100 个单选组和 100*4 单选按钮。所以我需要找出是否选择了同一个单选组中的相同单选按钮。换句话说,我需要确定选择的单选按钮(一组中的 4 个)是否来自同一组。任何帮助,将不胜感激。谢谢

【问题讨论】:

  • 尝试使用类似于键值对的Hashmap,它将帮助您将每个问题设置为键
  • 您想知道radioGroup 中的哪个radioButton 被选中了吗?

标签: android kotlin android-recyclerview radio-button radio-group


【解决方案1】:

你必须使用哈希映射来代表同一个单选组中单选按钮的每次点击 这个问题主要是因为你的Adapter对象重用了。

所以你应该做的是,你应该以某种方式存储选定的项目,并且当它在视图中调用 getView 方法时你应该检查和更新。

这里有一些参考代码:请关注并告诉我:

公共类 CustomArrayAdapter 扩展 BaseAdapter{

private Context context;
private ArrayList<HashMap<String, String>> data;

public CustomArrayAdapter(Context context,ArrayList<HashMap<String, String>> data) {
    super();
    this.context = context;
    this.data = data;

}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if(convertView==null){
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.custom_questions, parent,false);
        holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
        holder.radioGroup1 = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
        holder.radio0 = (RadioButton) convertView.findViewById(R.id.radio0);
        holder.radio1 = (RadioButton) convertView.findViewById(R.id.radio1);
        holder.radio2 = (RadioButton) convertView.findViewById(R.id.radio2);
        holder.radio3 = (RadioButton) convertView.findViewById(R.id.radio3);

        convertView.setTag(holder);
    }else{
        holder =(ViewHolder) convertView.getTag();
    }

    holder.textView1.setText(data.get(position).get("questions"));
    holder.radio0.setText(data.get(position).get("op1"));
    holder.radio1.setText(data.get(position).get("op2"));
    holder.radio2.setText(data.get(position).get("op3"));
    holder.radio3.setText(data.get(position).get("op4"));

    HashMap<Integer,String> radioMap = new HashMap<Integer, String>();
    radioMap.put(holder.radio0.getId(),holder.radio0.getText().toString());
    radioMap.put(holder.radio1.getId(),holder.radio1.getText().toString());
    radioMap.put(holder.radio2.getId(),holder.radio2.getText().toString());
    radioMap.put(holder.radio3.getId(),holder.radio3.getText().toString());

    holder.radioGroup1.setTag(radioMap);
    holder.radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            HashMap<Integer,String> data = (HashMap<Integer,String>) group.getTag();
            Toast.makeText(context,data.get(checkedId),Toast.LENGTH_SHORT).show();
        }
    });

    return convertView;
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

class ViewHolder{
    TextView textView1;
    RadioGroup radioGroup1;
    RadioButton radio0;
    RadioButton radio1;
    RadioButton radio2;
    RadioButton radio3;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    相关资源
    最近更新 更多