0

在我的活动中,我有viewstub来调用recycleview和recycleview中的其他布局我需要将skor从单选按钮插入到主要活动中

像这样

在此处输入图像描述

如果我在主布局中选择单选按钮 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);
        }
    }
}

这是我使用viewstub的main_activity

    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);
4

1 回答 1

2

首先在适配器内部创建接口:

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

然后在适配器内声明监听器,它应该从活动中传递:

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

在适配器内的 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

于 2020-05-08T10:25:27.437 回答