如何将适配器中的数据从单选按钮传递到 Activity

How to pass data in Adapter to Activity from radio button

在我的 activity 中,我有 viewstub 来调用 recycleview 和其他布局 在 recycleview 中,我需要将 skor 从 radiobutton 插入到 Main activity

像这样

如果我在主布局中选择单选按钮 skor 增加

这个我的绑定我只是使用文本因为我不知道发送 skor

   package com.sariazhariyah.eassessmentinteraktif;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

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

    String option[];

    public sikapadapter(String[] option) {
        this.option = option;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recycleview_sikap_layout,parent,false);
        sikapadapter.ViewHolder aadvh = new sikapadapter.ViewHolder(view);
        return aadvh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.text.setText(option[position]);
        int Radioid = holder.radio.getCheckedRadioButtonId();

    }

    @Override
    public int getItemCount() {
        return option.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView text;
        RadioGroup radio;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.recytexttampil);
            radio = itemView.findViewById(R.id.radiogroup);
        }
    }
}

这是 main_activity 我使用 viewstub

的地方
    String s1[];
        s1 = getResources().getStringArray(R.array.option);

        ViewStub viewStub = (ViewStub) findViewById(R.id.stub);
        //call layout where contain recycleview
        viewStub.setLayoutResource(R.layout.penilaian_sikap_layout);
        View inflatedView = viewStub.inflate();

        recycle = (RecyclerView) inflatedView.findViewById(R.id.recyclesikap);
        recycle.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recycle.setLayoutManager(layoutManager);
        mAdapter = new sikapadapter(s1);
        recycle.setAdapter(mAdapter);

首先在您的适配器中创建接口:

  interface OnRadioChangeListener{
        void onRadioChange(RadioGroup radioGroup,int checkedId);
    }

然后在适配器中声明监听器,它应该从 activity:

传递
private OnRadioChangeListener onRadioChangeListener;
public sikapadapter(String[] option,OnRadioChangeListener onRadioChangeListener) {
    this.option = option;
    this.onRadioChangeListener = onRadioChangeListener;
}

在您的 Adapter 中的 onBindViewHolder() 方法中:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.text.setText(option[position]);
    holder.radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(onRadioChangeListener!=null){
                onRadioChangeListener.onRadioChange(group,checkedId);
            }
        }
    });
    int Radioid = holder.radio.getCheckedRadioButtonId();
}

现在让我们转到您的 mainActivity:

无论你在哪里创建适配器,只需像这样添加监听器,例如:

skipadapter = new skipadapter (option,new skipadapter.OnRadioChangeListener(){
void onRadioChange(RadioGroup group,int checkedId){
//do whatever u wanna do here in your activity
}
});

顺便说一句,skipadapter 不是您应该遵循的命名约定,请将其更改为 SkipAdapter