in my activity i have viewstub to call recycleview & other layout in recycleview i need to insert skor from radiobutton to Main activity
like this
if i choose radiobutton skor inside main layout increase
this My bind i just use text cause i don't know to send 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);
}
}
}
this is main_activity where i use 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);
First create the interface inside your adapter:
interface OnRadioChangeListener{
void onRadioChange(RadioGroup radioGroup,int checkedId);
}
Then declare the listener inside the adapter, that should be passed from the activity:
private OnRadioChangeListener onRadioChangeListener;
public sikapadapter(String[] option,OnRadioChangeListener onRadioChangeListener) {
this.option = option;
this.onRadioChangeListener = onRadioChangeListener;
}
and in onBindViewHolder() method inside your Adapter:
@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();
}
Now let's go to your mainActivity:
wherever you are creasting the adapter, just add the listener like this, for example:
skipadapter = new skipadapter (option,new skipadapter.OnRadioChangeListener(){
void onRadioChange(RadioGroup group,int checkedId){
//do whatever u wanna do here in your activity
}
});
by the way, skipadapter
is not a naming convention that you should follow, change it to SkipAdapter