如何在回收视图中为单选按钮添加动作事件?

How to add an action event for RadioButton inside a recyler view?

我创建了一个 RecylerView,它允许教师将出勤详细信息添加到 firebase,我在一个单选组中创建了两个单选按钮,我尝试为它们添加一个事件侦听器,但没有成功。

回收者视图的实际视图

我的适配器代码

public class AtdListAdapter extends RecyclerView.Adapter<AtdListAdapter.ViewHolder> {

private List<String> atd ;
private Context context;
String str;
private DatabaseReference demo;

public AtdListAdapter(List<String> atd, Context context) {
    this.atd = atd;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.atdliststaff,null);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {


    str = atd.get(position);
    holder.name.setText(str);

}

@Override
public int getItemCount() {
    return atd.size();
}

public   class ViewHolder extends RecyclerView.ViewHolder{
    private  DatabaseReference demo;
    private   TextView name;
    private  RadioButton rb1,rb2;
    private  RadioGroup rg;

    public ViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.name);

        rg = itemView.findViewById(R.id.rg);
        rb1 = itemView.findViewById(R.id.rb1);
        rb2 = itemView.findViewById(R.id.rb2);

        if(rb1.isSelected()) {
            demo = FirebaseDatabase.getInstance().getReference().child("IDS").child("AtdList").child("1").child("1").child(str).child("status");
            demo.setValue("Present");
            Toast.makeText(context, "updated by Present", Toast.LENGTH_SHORT).show();

        }

        else if(rb2.isSelected())
        {
            demo = FirebaseDatabase.getInstance().getReference().child("IDS").child("AtdList").child("1").child("1").child(str).child("status");

            demo.setValue("Absent");
            Toast.makeText(context, "Updated by Absent", Toast.LENGTH_SHORT).show();

        }

   }
}
}

我也尝试在 onBindViewHolder() 方法中添加单选按钮的事件侦听器,但它也不起作用。

调用此方法首先你需要为单选按钮实现 Click 事件,

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.radio_pirates:
        if (checked)
            // Pirates are the best
        break;
    case R.id.radio_ninjas:
        if (checked)
            // Ninjas rule
        break;
}

}

Learn more here.

    Replace your holder with this :

public   class ViewHolder extends RecyclerView.ViewHolder{
    private  DatabaseReference demo;
    private   TextView name;
    private  RadioButton rb1,rb2;
    private  RadioGroup rg;

    public ViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.name);

        rg = itemView.findViewById(R.id.rg);
        rb1 = itemView.findViewById(R.id.rb1);
        rb2 = itemView.findViewById(R.id.rb2);

rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                   demo = FirebaseDatabase.getInstance().getReference().child("IDS").child("AtdList").child("1").child("1").child(str).child("status");
            demo.setValue("Present");
            Toast.makeText(context, "updated by Present", Toast.LENGTH_SHORT).show();
                }
            }
        });

     rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (b){
                demo = FirebaseDatabase.getInstance().getReference().child("IDS").child("AtdList").child("1").child("1").child(str).child("status");

            demo.setValue("Absent");
            Toast.makeText(context, "Updated by Absent", Toast.LENGTH_SHORT).show();
            }
        }
    });  

   }
}

试试这个代码.. 使接口进入适配器 class 如下所示..

  OnItemClick onItemClick;

public void setOnItemClick(OnItemClick onItemClick) {
    this.onItemClick = onItemClick;
}

public interface OnItemClick {
    void getPosition(boolean flag); //pass any things
}

在那之后..

@Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
    // below code handle click event on recycler view item.
    String data = mStringList.get(position);
    holder.textView.setText(data);
    holder.radiobutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClick.getPosition(true);
        }
    });
    holder.radiobutton2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClick.getPosition(true);
        }
    });


}

在适配器绑定到 recyclerview 之后,这意味着适配器不为空然后调用下面的代码..

        adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
        @Override
        public void getPosition(boolean flag) {
            if(flag) {
                demo = FirebaseDatabase.getInstance().getReference().child("IDS").child("AtdList").child("1").child("1").child(str).child("status");
                demo.setValue("Present");
                Toast.makeText(context, "updated by Present", Toast.LENGTH_SHORT).show();
                adpater.notifyDataSetChanged();
            }

            else if(flag)
            {
                demo = FirebaseDatabase.getInstance().getReference().child("IDS").child("AtdList").child("1").child("1").child(str).child("status");

                demo.setValue("Absent");
                Toast.makeText(context, "Updated by Absent", Toast.LENGTH_SHORT).show();
                adpater.notifyDataSetChanged();
            }
        }
    });