In a recyclerview how to determine the radio button clicked is from the same radio group in Android
I have a RadioGroup
with 4 RadioButtons
inside a RecyclerView
.
RecyclerView adapter goes like this
class QuestionRecyclerAdapter(var number:Int, val context: Context, val quesions: List<Question>,val qnADao: QnADao) : RecyclerView.Adapter<QuestionRecyclerAdapter.ViewHolder>() {
lateinit var question:Question
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.question_single_item, parent, false));
}
override fun getItemCount(): Int {
return quesions.size;
}
fun updateNumber(number: Int){
this.number = number
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
question = quesions[position]
holder.textTestQuestion.text = ""+(number+position)+" "+ question.question;
holder.answerFirst.text = "a. "+ question.answers?.get(0).toString()
holder.answerSecond.text = "b. "+ question.answers?.get(1).toString()
holder.answerThird.text = "c. "+ question.answers?.get(2).toString()
holder.answerFourth.text = "d. "+ question.answers?.get(3).toString()
holder.radio_group.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener{
override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
when(checkedId){
R.id.answerFirst -> {
question.rightAnswer=0
}
R.id.answerSecond -> {
question.rightAnswer=1
}
R.id.answerThird -> {
question.rightAnswer=2
}
R.id.answerFourth -> {
question.rightAnswer=3
}
}
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textTestQuestion = view.test_text_question
val answerFirst = view.answerFirst
val answerSecond = view.answerSecond
val answerThird = view.answerThird
val answerFourth = view.answerFourth
val radio_group = view.radioGrp
}
}
question_single_item.xml
is like this
<RadioButton
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/fourth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
</RadioGroup>
</RelativeLayout>
In the recyclerview I am populating almost 100s of data and so there are 100 radio groups with 100*4 radio buttons. So I need to find out if the same radio buttons in a same radio group is selected or not. In other words, I need to find out if the radio button(4 in a group) selected is from the same group or not. Any help would be appreciated. Thanks
android kotlin android-recyclerview radio-button radio-group
add a comment |
I have a RadioGroup
with 4 RadioButtons
inside a RecyclerView
.
RecyclerView adapter goes like this
class QuestionRecyclerAdapter(var number:Int, val context: Context, val quesions: List<Question>,val qnADao: QnADao) : RecyclerView.Adapter<QuestionRecyclerAdapter.ViewHolder>() {
lateinit var question:Question
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.question_single_item, parent, false));
}
override fun getItemCount(): Int {
return quesions.size;
}
fun updateNumber(number: Int){
this.number = number
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
question = quesions[position]
holder.textTestQuestion.text = ""+(number+position)+" "+ question.question;
holder.answerFirst.text = "a. "+ question.answers?.get(0).toString()
holder.answerSecond.text = "b. "+ question.answers?.get(1).toString()
holder.answerThird.text = "c. "+ question.answers?.get(2).toString()
holder.answerFourth.text = "d. "+ question.answers?.get(3).toString()
holder.radio_group.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener{
override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
when(checkedId){
R.id.answerFirst -> {
question.rightAnswer=0
}
R.id.answerSecond -> {
question.rightAnswer=1
}
R.id.answerThird -> {
question.rightAnswer=2
}
R.id.answerFourth -> {
question.rightAnswer=3
}
}
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textTestQuestion = view.test_text_question
val answerFirst = view.answerFirst
val answerSecond = view.answerSecond
val answerThird = view.answerThird
val answerFourth = view.answerFourth
val radio_group = view.radioGrp
}
}
question_single_item.xml
is like this
<RadioButton
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/fourth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
</RadioGroup>
</RelativeLayout>
In the recyclerview I am populating almost 100s of data and so there are 100 radio groups with 100*4 radio buttons. So I need to find out if the same radio buttons in a same radio group is selected or not. In other words, I need to find out if the radio button(4 in a group) selected is from the same group or not. Any help would be appreciated. Thanks
android kotlin android-recyclerview radio-button radio-group
try using Hashmap its similar to key value pair it will help you to set each question as key
– Ashish
Jan 1 at 16:00
Do you want to know which radioButton in a radioGroup isChecked?
– beigirad
Jan 1 at 16:28
add a comment |
I have a RadioGroup
with 4 RadioButtons
inside a RecyclerView
.
RecyclerView adapter goes like this
class QuestionRecyclerAdapter(var number:Int, val context: Context, val quesions: List<Question>,val qnADao: QnADao) : RecyclerView.Adapter<QuestionRecyclerAdapter.ViewHolder>() {
lateinit var question:Question
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.question_single_item, parent, false));
}
override fun getItemCount(): Int {
return quesions.size;
}
fun updateNumber(number: Int){
this.number = number
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
question = quesions[position]
holder.textTestQuestion.text = ""+(number+position)+" "+ question.question;
holder.answerFirst.text = "a. "+ question.answers?.get(0).toString()
holder.answerSecond.text = "b. "+ question.answers?.get(1).toString()
holder.answerThird.text = "c. "+ question.answers?.get(2).toString()
holder.answerFourth.text = "d. "+ question.answers?.get(3).toString()
holder.radio_group.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener{
override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
when(checkedId){
R.id.answerFirst -> {
question.rightAnswer=0
}
R.id.answerSecond -> {
question.rightAnswer=1
}
R.id.answerThird -> {
question.rightAnswer=2
}
R.id.answerFourth -> {
question.rightAnswer=3
}
}
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textTestQuestion = view.test_text_question
val answerFirst = view.answerFirst
val answerSecond = view.answerSecond
val answerThird = view.answerThird
val answerFourth = view.answerFourth
val radio_group = view.radioGrp
}
}
question_single_item.xml
is like this
<RadioButton
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/fourth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
</RadioGroup>
</RelativeLayout>
In the recyclerview I am populating almost 100s of data and so there are 100 radio groups with 100*4 radio buttons. So I need to find out if the same radio buttons in a same radio group is selected or not. In other words, I need to find out if the radio button(4 in a group) selected is from the same group or not. Any help would be appreciated. Thanks
android kotlin android-recyclerview radio-button radio-group
I have a RadioGroup
with 4 RadioButtons
inside a RecyclerView
.
RecyclerView adapter goes like this
class QuestionRecyclerAdapter(var number:Int, val context: Context, val quesions: List<Question>,val qnADao: QnADao) : RecyclerView.Adapter<QuestionRecyclerAdapter.ViewHolder>() {
lateinit var question:Question
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.question_single_item, parent, false));
}
override fun getItemCount(): Int {
return quesions.size;
}
fun updateNumber(number: Int){
this.number = number
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
question = quesions[position]
holder.textTestQuestion.text = ""+(number+position)+" "+ question.question;
holder.answerFirst.text = "a. "+ question.answers?.get(0).toString()
holder.answerSecond.text = "b. "+ question.answers?.get(1).toString()
holder.answerThird.text = "c. "+ question.answers?.get(2).toString()
holder.answerFourth.text = "d. "+ question.answers?.get(3).toString()
holder.radio_group.setOnCheckedChangeListener(object : RadioGroup.OnCheckedChangeListener{
override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
when(checkedId){
R.id.answerFirst -> {
question.rightAnswer=0
}
R.id.answerSecond -> {
question.rightAnswer=1
}
R.id.answerThird -> {
question.rightAnswer=2
}
R.id.answerFourth -> {
question.rightAnswer=3
}
}
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textTestQuestion = view.test_text_question
val answerFirst = view.answerFirst
val answerSecond = view.answerSecond
val answerThird = view.answerThird
val answerFourth = view.answerFourth
val radio_group = view.radioGrp
}
}
question_single_item.xml
is like this
<RadioButton
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
<RadioButton
android:id="@+id/fourth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableRight="@android:drawable/btn_radio"
android:textSize="14dp" />
</RadioGroup>
</RelativeLayout>
In the recyclerview I am populating almost 100s of data and so there are 100 radio groups with 100*4 radio buttons. So I need to find out if the same radio buttons in a same radio group is selected or not. In other words, I need to find out if the radio button(4 in a group) selected is from the same group or not. Any help would be appreciated. Thanks
android kotlin android-recyclerview radio-button radio-group
android kotlin android-recyclerview radio-button radio-group
asked Jan 1 at 15:36
theanilpaudeltheanilpaudel
1,09921236
1,09921236
try using Hashmap its similar to key value pair it will help you to set each question as key
– Ashish
Jan 1 at 16:00
Do you want to know which radioButton in a radioGroup isChecked?
– beigirad
Jan 1 at 16:28
add a comment |
try using Hashmap its similar to key value pair it will help you to set each question as key
– Ashish
Jan 1 at 16:00
Do you want to know which radioButton in a radioGroup isChecked?
– beigirad
Jan 1 at 16:28
try using Hashmap its similar to key value pair it will help you to set each question as key
– Ashish
Jan 1 at 16:00
try using Hashmap its similar to key value pair it will help you to set each question as key
– Ashish
Jan 1 at 16:00
Do you want to know which radioButton in a radioGroup isChecked?
– beigirad
Jan 1 at 16:28
Do you want to know which radioButton in a radioGroup isChecked?
– beigirad
Jan 1 at 16:28
add a comment |
1 Answer
1
active
oldest
votes
you have to use hash map on the behalf of every click of radio button in the same radio group
This problem is mainly because of your Adapter object reusing.
So what you should do is, You should store the selected item some how and you should check and update when ever it called getView method in the view.
here is some code for refrence:please follow and let me know :
public class CustomArrayAdapter extends BaseAdapter{
private Context context;
private ArrayList<HashMap<String, String>> data;
public CustomArrayAdapter(Context context,ArrayList<HashMap<String, String>> data) {
super();
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.custom_questions, parent,false);
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.radioGroup1 = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
holder.radio0 = (RadioButton) convertView.findViewById(R.id.radio0);
holder.radio1 = (RadioButton) convertView.findViewById(R.id.radio1);
holder.radio2 = (RadioButton) convertView.findViewById(R.id.radio2);
holder.radio3 = (RadioButton) convertView.findViewById(R.id.radio3);
convertView.setTag(holder);
}else{
holder =(ViewHolder) convertView.getTag();
}
holder.textView1.setText(data.get(position).get("questions"));
holder.radio0.setText(data.get(position).get("op1"));
holder.radio1.setText(data.get(position).get("op2"));
holder.radio2.setText(data.get(position).get("op3"));
holder.radio3.setText(data.get(position).get("op4"));
HashMap<Integer,String> radioMap = new HashMap<Integer, String>();
radioMap.put(holder.radio0.getId(),holder.radio0.getText().toString());
radioMap.put(holder.radio1.getId(),holder.radio1.getText().toString());
radioMap.put(holder.radio2.getId(),holder.radio2.getText().toString());
radioMap.put(holder.radio3.getId(),holder.radio3.getText().toString());
holder.radioGroup1.setTag(radioMap);
holder.radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
HashMap<Integer,String> data = (HashMap<Integer,String>) group.getTag();
Toast.makeText(context,data.get(checkedId),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder{
TextView textView1;
RadioGroup radioGroup1;
RadioButton radio0;
RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996723%2fin-a-recyclerview-how-to-determine-the-radio-button-clicked-is-from-the-same-rad%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you have to use hash map on the behalf of every click of radio button in the same radio group
This problem is mainly because of your Adapter object reusing.
So what you should do is, You should store the selected item some how and you should check and update when ever it called getView method in the view.
here is some code for refrence:please follow and let me know :
public class CustomArrayAdapter extends BaseAdapter{
private Context context;
private ArrayList<HashMap<String, String>> data;
public CustomArrayAdapter(Context context,ArrayList<HashMap<String, String>> data) {
super();
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.custom_questions, parent,false);
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.radioGroup1 = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
holder.radio0 = (RadioButton) convertView.findViewById(R.id.radio0);
holder.radio1 = (RadioButton) convertView.findViewById(R.id.radio1);
holder.radio2 = (RadioButton) convertView.findViewById(R.id.radio2);
holder.radio3 = (RadioButton) convertView.findViewById(R.id.radio3);
convertView.setTag(holder);
}else{
holder =(ViewHolder) convertView.getTag();
}
holder.textView1.setText(data.get(position).get("questions"));
holder.radio0.setText(data.get(position).get("op1"));
holder.radio1.setText(data.get(position).get("op2"));
holder.radio2.setText(data.get(position).get("op3"));
holder.radio3.setText(data.get(position).get("op4"));
HashMap<Integer,String> radioMap = new HashMap<Integer, String>();
radioMap.put(holder.radio0.getId(),holder.radio0.getText().toString());
radioMap.put(holder.radio1.getId(),holder.radio1.getText().toString());
radioMap.put(holder.radio2.getId(),holder.radio2.getText().toString());
radioMap.put(holder.radio3.getId(),holder.radio3.getText().toString());
holder.radioGroup1.setTag(radioMap);
holder.radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
HashMap<Integer,String> data = (HashMap<Integer,String>) group.getTag();
Toast.makeText(context,data.get(checkedId),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder{
TextView textView1;
RadioGroup radioGroup1;
RadioButton radio0;
RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
}
}
add a comment |
you have to use hash map on the behalf of every click of radio button in the same radio group
This problem is mainly because of your Adapter object reusing.
So what you should do is, You should store the selected item some how and you should check and update when ever it called getView method in the view.
here is some code for refrence:please follow and let me know :
public class CustomArrayAdapter extends BaseAdapter{
private Context context;
private ArrayList<HashMap<String, String>> data;
public CustomArrayAdapter(Context context,ArrayList<HashMap<String, String>> data) {
super();
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.custom_questions, parent,false);
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.radioGroup1 = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
holder.radio0 = (RadioButton) convertView.findViewById(R.id.radio0);
holder.radio1 = (RadioButton) convertView.findViewById(R.id.radio1);
holder.radio2 = (RadioButton) convertView.findViewById(R.id.radio2);
holder.radio3 = (RadioButton) convertView.findViewById(R.id.radio3);
convertView.setTag(holder);
}else{
holder =(ViewHolder) convertView.getTag();
}
holder.textView1.setText(data.get(position).get("questions"));
holder.radio0.setText(data.get(position).get("op1"));
holder.radio1.setText(data.get(position).get("op2"));
holder.radio2.setText(data.get(position).get("op3"));
holder.radio3.setText(data.get(position).get("op4"));
HashMap<Integer,String> radioMap = new HashMap<Integer, String>();
radioMap.put(holder.radio0.getId(),holder.radio0.getText().toString());
radioMap.put(holder.radio1.getId(),holder.radio1.getText().toString());
radioMap.put(holder.radio2.getId(),holder.radio2.getText().toString());
radioMap.put(holder.radio3.getId(),holder.radio3.getText().toString());
holder.radioGroup1.setTag(radioMap);
holder.radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
HashMap<Integer,String> data = (HashMap<Integer,String>) group.getTag();
Toast.makeText(context,data.get(checkedId),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder{
TextView textView1;
RadioGroup radioGroup1;
RadioButton radio0;
RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
}
}
add a comment |
you have to use hash map on the behalf of every click of radio button in the same radio group
This problem is mainly because of your Adapter object reusing.
So what you should do is, You should store the selected item some how and you should check and update when ever it called getView method in the view.
here is some code for refrence:please follow and let me know :
public class CustomArrayAdapter extends BaseAdapter{
private Context context;
private ArrayList<HashMap<String, String>> data;
public CustomArrayAdapter(Context context,ArrayList<HashMap<String, String>> data) {
super();
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.custom_questions, parent,false);
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.radioGroup1 = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
holder.radio0 = (RadioButton) convertView.findViewById(R.id.radio0);
holder.radio1 = (RadioButton) convertView.findViewById(R.id.radio1);
holder.radio2 = (RadioButton) convertView.findViewById(R.id.radio2);
holder.radio3 = (RadioButton) convertView.findViewById(R.id.radio3);
convertView.setTag(holder);
}else{
holder =(ViewHolder) convertView.getTag();
}
holder.textView1.setText(data.get(position).get("questions"));
holder.radio0.setText(data.get(position).get("op1"));
holder.radio1.setText(data.get(position).get("op2"));
holder.radio2.setText(data.get(position).get("op3"));
holder.radio3.setText(data.get(position).get("op4"));
HashMap<Integer,String> radioMap = new HashMap<Integer, String>();
radioMap.put(holder.radio0.getId(),holder.radio0.getText().toString());
radioMap.put(holder.radio1.getId(),holder.radio1.getText().toString());
radioMap.put(holder.radio2.getId(),holder.radio2.getText().toString());
radioMap.put(holder.radio3.getId(),holder.radio3.getText().toString());
holder.radioGroup1.setTag(radioMap);
holder.radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
HashMap<Integer,String> data = (HashMap<Integer,String>) group.getTag();
Toast.makeText(context,data.get(checkedId),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder{
TextView textView1;
RadioGroup radioGroup1;
RadioButton radio0;
RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
}
}
you have to use hash map on the behalf of every click of radio button in the same radio group
This problem is mainly because of your Adapter object reusing.
So what you should do is, You should store the selected item some how and you should check and update when ever it called getView method in the view.
here is some code for refrence:please follow and let me know :
public class CustomArrayAdapter extends BaseAdapter{
private Context context;
private ArrayList<HashMap<String, String>> data;
public CustomArrayAdapter(Context context,ArrayList<HashMap<String, String>> data) {
super();
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.custom_questions, parent,false);
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.radioGroup1 = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
holder.radio0 = (RadioButton) convertView.findViewById(R.id.radio0);
holder.radio1 = (RadioButton) convertView.findViewById(R.id.radio1);
holder.radio2 = (RadioButton) convertView.findViewById(R.id.radio2);
holder.radio3 = (RadioButton) convertView.findViewById(R.id.radio3);
convertView.setTag(holder);
}else{
holder =(ViewHolder) convertView.getTag();
}
holder.textView1.setText(data.get(position).get("questions"));
holder.radio0.setText(data.get(position).get("op1"));
holder.radio1.setText(data.get(position).get("op2"));
holder.radio2.setText(data.get(position).get("op3"));
holder.radio3.setText(data.get(position).get("op4"));
HashMap<Integer,String> radioMap = new HashMap<Integer, String>();
radioMap.put(holder.radio0.getId(),holder.radio0.getText().toString());
radioMap.put(holder.radio1.getId(),holder.radio1.getText().toString());
radioMap.put(holder.radio2.getId(),holder.radio2.getText().toString());
radioMap.put(holder.radio3.getId(),holder.radio3.getText().toString());
holder.radioGroup1.setTag(radioMap);
holder.radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
HashMap<Integer,String> data = (HashMap<Integer,String>) group.getTag();
Toast.makeText(context,data.get(checkedId),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder{
TextView textView1;
RadioGroup radioGroup1;
RadioButton radio0;
RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
}
}
answered Jan 2 at 14:25
Shalu GuptaShalu Gupta
3729
3729
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996723%2fin-a-recyclerview-how-to-determine-the-radio-button-clicked-is-from-the-same-rad%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
try using Hashmap its similar to key value pair it will help you to set each question as key
– Ashish
Jan 1 at 16:00
Do you want to know which radioButton in a radioGroup isChecked?
– beigirad
Jan 1 at 16:28