In my application am displaying 20 multiple choice questions with the help of RecyclerView
.
If I change the value of first RadioGroup
and scrolls down, again scrolls up removing the selected value in RecycelarView
and also i want to use that selected RadioButton
value further, I was also checked link1 but i did't understood what he is doing.
Here is my sample code snippet let me know if you need any clarification.
package com.testing.survey;
import java.util.List;
import android.annotation.SuppressLint;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
@SuppressWarnings("rawtypes")
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private List<Student> stList;
public DataAdapter(List<Student> students) {
this.stList = students;
}
// Create new views
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_row, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@SuppressLint("UseValueOf") @Override
public void onBindViewHolder(ViewHolder viewHolder,int position) {
final int pos = position;
viewHolder.tvQuestionNumber.setText(stList.get(position).getQuestionNumber());
viewHolder.tvQuestion.setText(stList.get(position).getQuestion());
viewHolder.rbAns1.setText(stList.get(position).getAnswer1());
viewHolder.rbAns2.setText(stList.get(position).getAnswer2());
viewHolder.rbAns3.setText(stList.get(position).getAnswer3());
viewHolder.rbAns4.setText(stList.get(position).getAnswer4());
viewHolder.rbAns5.setText(stList.get(position).getAnswer4());
//viewHolder.rgAnswers.clearCheck();
viewHolder.rgAnswers.check(stList.get(position).getSelectedRadioButtonId());
viewHolder.rgAnswers.setTag(new Integer(position));
Log.v("select"+position,stList.get(position).getSelectedRadioButtonId()+"");
viewHolder.rgAnswers.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
int radioButtonID = group.getCheckedRadioButtonId();
View radioButton = group.findViewById(radioButtonID);
int clickedPos = ((Integer)group.getTag()).intValue();
//Student contact=(Student)group.getTag();
//contact.setSelectedRadioButtonId(radioButtonID);
stList.get(clickedPos).setSelectedRadioButtonId(radioButtonID);
Log.v("hello"+clickedPos,stList.get(clickedPos).getSelectedRadioButtonId()+"");
}
});
}
// Return the size arraylist
@Override
public int getItemCount() {
return stList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvQuestionNumber;
public TextView tvQuestion;
public RadioGroup rgAnswers;
public RadioButton rbAns1,rbAns2,rbAns3,rbAns4,rbAns5;
public Student singlestudent;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
tvQuestionNumber = (TextView) itemLayoutView.findViewById(R.id.tvQuestionNumber);
tvQuestion = (TextView) itemLayoutView.findViewById(R.id.tvQuestion);
rgAnswers=(RadioGroup)itemLayoutView.findViewById(R.id.rgAnswers);
rbAns1=(RadioButton)itemLayoutView.findViewById(R.id.rbAnswer1);
rbAns2=(RadioButton)itemLayoutView.findViewById(R.id.rbAnswer2);
rbAns3=(RadioButton)itemLayoutView.findViewById(R.id.rbAnswer3);
rbAns4=(RadioButton)itemLayoutView.findViewById(R.id.rbAnswer4);
rbAns5=(RadioButton)itemLayoutView.findViewById(R.id.rbAnswer5);
}
}
// method to access in activity after updating selection
public List<Student> getStudentist() {
return stList;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…