Select one checkBox in item from recyclerView and deselect previously selected checkBox
I have recycler-view with sections. The item include row and title. I want to select one item with checkbox and if user select next one I want to deselect the old the one(checkbox). I cannot find anywhere to achieve this functionality.
Here is my code
recyclerView = (RecyclerView)findViewById(R.id.recyclerViewActivityExample);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
list = new ArrayList<>();
list.add(new AnimalObject("Cat","Mammals", false));
list.add(new AnimalObject("Lion","Mammals", false));
list.add(new AnimalObject("Dog","Mammals", false));
list.add(new AnimalObject("Monkey","Mammals", false));
list.add(new AnimalObject("Puma","Mammals", false));
list.add(new AnimalObject("Albatross","Birds", false));
list.add(new AnimalObject("Pigeon","Birds", false));
list.add(new AnimalObject("Crabs","Aquatic Animals", false));
list.add(new AnimalObject("Sharks","Aquatic Animals", false));
MyAdapter myAdapter = new MyAdapter();
sectionedRecyclerViewAdapter = new SectionedRecyclerViewAdapter(getApplicationContext(),
R.layout.layout_list_section, R.id.textViewItemSection, myAdapter, this);
sectionedRecyclerViewAdapter.setSections(list);
recyclerView.setAdapter(sectionedRecyclerViewAdapter);
}
//-------------------Adapter----------------------------
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> implements MyViewHolder.ViewHolderClickListener {
@Override
public int getItemCount() {
return list.size();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.layout_list_item, parent, false);
return new MyViewHolder(itemView,this);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AnimalObject animalObject = list.get(position);
holder.title.setText(animalObject.name);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checkbox.setChecked(true);
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
}
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), list.get(sectionedRecyclerViewAdapter.getIndexForPosition(recyclerView.getChildPosition(v))).name,Toast.LENGTH_SHORT).show();
}
}
// Adapter
public String name;
public String type;
public boolean ischecked ;
public AnimalObject(final String name, final String type, boolean ischecked){
this.name = name ;
this.type = type ;
this.ischecked = ischecked;
}
public boolean ischecked() {
return ischecked;
}
public void setIschecked(boolean ischecked) {
this.ischecked = ischecked;
}
java

|
show 1 more comment
I have recycler-view with sections. The item include row and title. I want to select one item with checkbox and if user select next one I want to deselect the old the one(checkbox). I cannot find anywhere to achieve this functionality.
Here is my code
recyclerView = (RecyclerView)findViewById(R.id.recyclerViewActivityExample);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
list = new ArrayList<>();
list.add(new AnimalObject("Cat","Mammals", false));
list.add(new AnimalObject("Lion","Mammals", false));
list.add(new AnimalObject("Dog","Mammals", false));
list.add(new AnimalObject("Monkey","Mammals", false));
list.add(new AnimalObject("Puma","Mammals", false));
list.add(new AnimalObject("Albatross","Birds", false));
list.add(new AnimalObject("Pigeon","Birds", false));
list.add(new AnimalObject("Crabs","Aquatic Animals", false));
list.add(new AnimalObject("Sharks","Aquatic Animals", false));
MyAdapter myAdapter = new MyAdapter();
sectionedRecyclerViewAdapter = new SectionedRecyclerViewAdapter(getApplicationContext(),
R.layout.layout_list_section, R.id.textViewItemSection, myAdapter, this);
sectionedRecyclerViewAdapter.setSections(list);
recyclerView.setAdapter(sectionedRecyclerViewAdapter);
}
//-------------------Adapter----------------------------
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> implements MyViewHolder.ViewHolderClickListener {
@Override
public int getItemCount() {
return list.size();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.layout_list_item, parent, false);
return new MyViewHolder(itemView,this);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AnimalObject animalObject = list.get(position);
holder.title.setText(animalObject.name);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checkbox.setChecked(true);
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
}
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), list.get(sectionedRecyclerViewAdapter.getIndexForPosition(recyclerView.getChildPosition(v))).name,Toast.LENGTH_SHORT).show();
}
}
// Adapter
public String name;
public String type;
public boolean ischecked ;
public AnimalObject(final String name, final String type, boolean ischecked){
this.name = name ;
this.type = type ;
this.ischecked = ischecked;
}
public boolean ischecked() {
return ischecked;
}
public void setIschecked(boolean ischecked) {
this.ischecked = ischecked;
}
java

It's Single item choice selection. You can manage it by declaring position = -1 and check your adapter position while clicking on it.
– Piyush
Nov 22 '18 at 9:56
It is very likely the problem is within the adapter, can you post the code of your adapter?
– Aaron
Nov 22 '18 at 9:59
@Piyush I am not sure how to tell which checkbox to setSelected false.
– cole
Nov 22 '18 at 10:06
@Aaron I updated my question with adapter class added
– cole
Nov 22 '18 at 10:06
@code Weird.. I don't see them.. and your ViewHolder is incomplete, anyway I just want to make sure that you bind the views correctly.
– Aaron
Nov 22 '18 at 10:09
|
show 1 more comment
I have recycler-view with sections. The item include row and title. I want to select one item with checkbox and if user select next one I want to deselect the old the one(checkbox). I cannot find anywhere to achieve this functionality.
Here is my code
recyclerView = (RecyclerView)findViewById(R.id.recyclerViewActivityExample);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
list = new ArrayList<>();
list.add(new AnimalObject("Cat","Mammals", false));
list.add(new AnimalObject("Lion","Mammals", false));
list.add(new AnimalObject("Dog","Mammals", false));
list.add(new AnimalObject("Monkey","Mammals", false));
list.add(new AnimalObject("Puma","Mammals", false));
list.add(new AnimalObject("Albatross","Birds", false));
list.add(new AnimalObject("Pigeon","Birds", false));
list.add(new AnimalObject("Crabs","Aquatic Animals", false));
list.add(new AnimalObject("Sharks","Aquatic Animals", false));
MyAdapter myAdapter = new MyAdapter();
sectionedRecyclerViewAdapter = new SectionedRecyclerViewAdapter(getApplicationContext(),
R.layout.layout_list_section, R.id.textViewItemSection, myAdapter, this);
sectionedRecyclerViewAdapter.setSections(list);
recyclerView.setAdapter(sectionedRecyclerViewAdapter);
}
//-------------------Adapter----------------------------
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> implements MyViewHolder.ViewHolderClickListener {
@Override
public int getItemCount() {
return list.size();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.layout_list_item, parent, false);
return new MyViewHolder(itemView,this);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AnimalObject animalObject = list.get(position);
holder.title.setText(animalObject.name);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checkbox.setChecked(true);
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
}
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), list.get(sectionedRecyclerViewAdapter.getIndexForPosition(recyclerView.getChildPosition(v))).name,Toast.LENGTH_SHORT).show();
}
}
// Adapter
public String name;
public String type;
public boolean ischecked ;
public AnimalObject(final String name, final String type, boolean ischecked){
this.name = name ;
this.type = type ;
this.ischecked = ischecked;
}
public boolean ischecked() {
return ischecked;
}
public void setIschecked(boolean ischecked) {
this.ischecked = ischecked;
}
java

I have recycler-view with sections. The item include row and title. I want to select one item with checkbox and if user select next one I want to deselect the old the one(checkbox). I cannot find anywhere to achieve this functionality.
Here is my code
recyclerView = (RecyclerView)findViewById(R.id.recyclerViewActivityExample);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
list = new ArrayList<>();
list.add(new AnimalObject("Cat","Mammals", false));
list.add(new AnimalObject("Lion","Mammals", false));
list.add(new AnimalObject("Dog","Mammals", false));
list.add(new AnimalObject("Monkey","Mammals", false));
list.add(new AnimalObject("Puma","Mammals", false));
list.add(new AnimalObject("Albatross","Birds", false));
list.add(new AnimalObject("Pigeon","Birds", false));
list.add(new AnimalObject("Crabs","Aquatic Animals", false));
list.add(new AnimalObject("Sharks","Aquatic Animals", false));
MyAdapter myAdapter = new MyAdapter();
sectionedRecyclerViewAdapter = new SectionedRecyclerViewAdapter(getApplicationContext(),
R.layout.layout_list_section, R.id.textViewItemSection, myAdapter, this);
sectionedRecyclerViewAdapter.setSections(list);
recyclerView.setAdapter(sectionedRecyclerViewAdapter);
}
//-------------------Adapter----------------------------
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> implements MyViewHolder.ViewHolderClickListener {
@Override
public int getItemCount() {
return list.size();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.layout_list_item, parent, false);
return new MyViewHolder(itemView,this);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AnimalObject animalObject = list.get(position);
holder.title.setText(animalObject.name);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checkbox.setChecked(true);
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}
}
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), list.get(sectionedRecyclerViewAdapter.getIndexForPosition(recyclerView.getChildPosition(v))).name,Toast.LENGTH_SHORT).show();
}
}
// Adapter
public String name;
public String type;
public boolean ischecked ;
public AnimalObject(final String name, final String type, boolean ischecked){
this.name = name ;
this.type = type ;
this.ischecked = ischecked;
}
public boolean ischecked() {
return ischecked;
}
public void setIschecked(boolean ischecked) {
this.ischecked = ischecked;
}
java

java

edited Nov 22 '18 at 10:18
cole
asked Nov 22 '18 at 9:54
colecole
1,1722817
1,1722817
It's Single item choice selection. You can manage it by declaring position = -1 and check your adapter position while clicking on it.
– Piyush
Nov 22 '18 at 9:56
It is very likely the problem is within the adapter, can you post the code of your adapter?
– Aaron
Nov 22 '18 at 9:59
@Piyush I am not sure how to tell which checkbox to setSelected false.
– cole
Nov 22 '18 at 10:06
@Aaron I updated my question with adapter class added
– cole
Nov 22 '18 at 10:06
@code Weird.. I don't see them.. and your ViewHolder is incomplete, anyway I just want to make sure that you bind the views correctly.
– Aaron
Nov 22 '18 at 10:09
|
show 1 more comment
It's Single item choice selection. You can manage it by declaring position = -1 and check your adapter position while clicking on it.
– Piyush
Nov 22 '18 at 9:56
It is very likely the problem is within the adapter, can you post the code of your adapter?
– Aaron
Nov 22 '18 at 9:59
@Piyush I am not sure how to tell which checkbox to setSelected false.
– cole
Nov 22 '18 at 10:06
@Aaron I updated my question with adapter class added
– cole
Nov 22 '18 at 10:06
@code Weird.. I don't see them.. and your ViewHolder is incomplete, anyway I just want to make sure that you bind the views correctly.
– Aaron
Nov 22 '18 at 10:09
It's Single item choice selection. You can manage it by declaring position = -1 and check your adapter position while clicking on it.
– Piyush
Nov 22 '18 at 9:56
It's Single item choice selection. You can manage it by declaring position = -1 and check your adapter position while clicking on it.
– Piyush
Nov 22 '18 at 9:56
It is very likely the problem is within the adapter, can you post the code of your adapter?
– Aaron
Nov 22 '18 at 9:59
It is very likely the problem is within the adapter, can you post the code of your adapter?
– Aaron
Nov 22 '18 at 9:59
@Piyush I am not sure how to tell which checkbox to setSelected false.
– cole
Nov 22 '18 at 10:06
@Piyush I am not sure how to tell which checkbox to setSelected false.
– cole
Nov 22 '18 at 10:06
@Aaron I updated my question with adapter class added
– cole
Nov 22 '18 at 10:06
@Aaron I updated my question with adapter class added
– cole
Nov 22 '18 at 10:06
@code Weird.. I don't see them.. and your ViewHolder is incomplete, anyway I just want to make sure that you bind the views correctly.
– Aaron
Nov 22 '18 at 10:09
@code Weird.. I don't see them.. and your ViewHolder is incomplete, anyway I just want to make sure that you bind the views correctly.
– Aaron
Nov 22 '18 at 10:09
|
show 1 more comment
2 Answers
2
active
oldest
votes
Inside your Adapter
use this :private int row_index;
@Override
public void onBindViewHolder(final Main_Page_Payment_Cash_Adapter.ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checkbox.setChecked(true);
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
// checkbox.setChecked(true);
} else {
// checkbox.setChecked(false);
}
}
I hope this is what you are looking for.
Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
– cole
Nov 22 '18 at 10:17
1
Managed to run your code and its working now !! Thanks and enjoy some good points.
– cole
Nov 22 '18 at 10:24
what didnt work for you to update the answer to help the others :)
– Hossam Hassan
Nov 22 '18 at 10:47
add a comment |
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AnimalObject animalObject = list.get(position);
holder.title.setText(animalObject.name);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimalObject animalObject = list.get(position);
int currentCheckedStatus = animalObject.ischecked();
for (int i =0;i<list.size();i++)
{
if (i==position)
{
animalObject.setIschecked(!currentCheckedStatus);
}
else
{
animalObject.setIschecked(false);
}
}
notifyDataSetChanged();
}
});
holder.checkBox.setChecked(animalObject.ischecked());
}
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%2f53428224%2fselect-one-checkbox-in-item-from-recyclerview-and-deselect-previously-selected-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Inside your Adapter
use this :private int row_index;
@Override
public void onBindViewHolder(final Main_Page_Payment_Cash_Adapter.ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checkbox.setChecked(true);
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
// checkbox.setChecked(true);
} else {
// checkbox.setChecked(false);
}
}
I hope this is what you are looking for.
Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
– cole
Nov 22 '18 at 10:17
1
Managed to run your code and its working now !! Thanks and enjoy some good points.
– cole
Nov 22 '18 at 10:24
what didnt work for you to update the answer to help the others :)
– Hossam Hassan
Nov 22 '18 at 10:47
add a comment |
Inside your Adapter
use this :private int row_index;
@Override
public void onBindViewHolder(final Main_Page_Payment_Cash_Adapter.ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checkbox.setChecked(true);
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
// checkbox.setChecked(true);
} else {
// checkbox.setChecked(false);
}
}
I hope this is what you are looking for.
Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
– cole
Nov 22 '18 at 10:17
1
Managed to run your code and its working now !! Thanks and enjoy some good points.
– cole
Nov 22 '18 at 10:24
what didnt work for you to update the answer to help the others :)
– Hossam Hassan
Nov 22 '18 at 10:47
add a comment |
Inside your Adapter
use this :private int row_index;
@Override
public void onBindViewHolder(final Main_Page_Payment_Cash_Adapter.ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checkbox.setChecked(true);
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
// checkbox.setChecked(true);
} else {
// checkbox.setChecked(false);
}
}
I hope this is what you are looking for.
Inside your Adapter
use this :private int row_index;
@Override
public void onBindViewHolder(final Main_Page_Payment_Cash_Adapter.ViewHolder holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checkbox.setChecked(true);
row_index = position;
notifyDataSetChanged();
}
});
if (row_index == position) {
// checkbox.setChecked(true);
} else {
// checkbox.setChecked(false);
}
}
I hope this is what you are looking for.
answered Nov 22 '18 at 10:05


Hossam HassanHossam Hassan
2581319
2581319
Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
– cole
Nov 22 '18 at 10:17
1
Managed to run your code and its working now !! Thanks and enjoy some good points.
– cole
Nov 22 '18 at 10:24
what didnt work for you to update the answer to help the others :)
– Hossam Hassan
Nov 22 '18 at 10:47
add a comment |
Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
– cole
Nov 22 '18 at 10:17
1
Managed to run your code and its working now !! Thanks and enjoy some good points.
– cole
Nov 22 '18 at 10:24
what didnt work for you to update the answer to help the others :)
– Hossam Hassan
Nov 22 '18 at 10:47
Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
– cole
Nov 22 '18 at 10:17
Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
– cole
Nov 22 '18 at 10:17
1
1
Managed to run your code and its working now !! Thanks and enjoy some good points.
– cole
Nov 22 '18 at 10:24
Managed to run your code and its working now !! Thanks and enjoy some good points.
– cole
Nov 22 '18 at 10:24
what didnt work for you to update the answer to help the others :)
– Hossam Hassan
Nov 22 '18 at 10:47
what didnt work for you to update the answer to help the others :)
– Hossam Hassan
Nov 22 '18 at 10:47
add a comment |
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AnimalObject animalObject = list.get(position);
holder.title.setText(animalObject.name);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimalObject animalObject = list.get(position);
int currentCheckedStatus = animalObject.ischecked();
for (int i =0;i<list.size();i++)
{
if (i==position)
{
animalObject.setIschecked(!currentCheckedStatus);
}
else
{
animalObject.setIschecked(false);
}
}
notifyDataSetChanged();
}
});
holder.checkBox.setChecked(animalObject.ischecked());
}
add a comment |
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AnimalObject animalObject = list.get(position);
holder.title.setText(animalObject.name);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimalObject animalObject = list.get(position);
int currentCheckedStatus = animalObject.ischecked();
for (int i =0;i<list.size();i++)
{
if (i==position)
{
animalObject.setIschecked(!currentCheckedStatus);
}
else
{
animalObject.setIschecked(false);
}
}
notifyDataSetChanged();
}
});
holder.checkBox.setChecked(animalObject.ischecked());
}
add a comment |
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AnimalObject animalObject = list.get(position);
holder.title.setText(animalObject.name);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimalObject animalObject = list.get(position);
int currentCheckedStatus = animalObject.ischecked();
for (int i =0;i<list.size();i++)
{
if (i==position)
{
animalObject.setIschecked(!currentCheckedStatus);
}
else
{
animalObject.setIschecked(false);
}
}
notifyDataSetChanged();
}
});
holder.checkBox.setChecked(animalObject.ischecked());
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
AnimalObject animalObject = list.get(position);
holder.title.setText(animalObject.name);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimalObject animalObject = list.get(position);
int currentCheckedStatus = animalObject.ischecked();
for (int i =0;i<list.size();i++)
{
if (i==position)
{
animalObject.setIschecked(!currentCheckedStatus);
}
else
{
animalObject.setIschecked(false);
}
}
notifyDataSetChanged();
}
});
holder.checkBox.setChecked(animalObject.ischecked());
}
answered Nov 22 '18 at 10:30
Farman Ali KhanFarman Ali Khan
310311
310311
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%2f53428224%2fselect-one-checkbox-in-item-from-recyclerview-and-deselect-previously-selected-c%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
It's Single item choice selection. You can manage it by declaring position = -1 and check your adapter position while clicking on it.
– Piyush
Nov 22 '18 at 9:56
It is very likely the problem is within the adapter, can you post the code of your adapter?
– Aaron
Nov 22 '18 at 9:59
@Piyush I am not sure how to tell which checkbox to setSelected false.
– cole
Nov 22 '18 at 10:06
@Aaron I updated my question with adapter class added
– cole
Nov 22 '18 at 10:06
@code Weird.. I don't see them.. and your ViewHolder is incomplete, anyway I just want to make sure that you bind the views correctly.
– Aaron
Nov 22 '18 at 10:09