Select one checkBox in item from recyclerView and deselect previously selected checkBox











up vote
1
down vote

favorite












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;
}









share|improve this question
























  • 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 at 9:56










  • It is very likely the problem is within the adapter, can you post the code of your adapter?
    – Aaron
    Nov 22 at 9:59










  • @Piyush I am not sure how to tell which checkbox to setSelected false.
    – cole
    Nov 22 at 10:06










  • @Aaron I updated my question with adapter class added
    – cole
    Nov 22 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 at 10:09















up vote
1
down vote

favorite












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;
}









share|improve this question
























  • 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 at 9:56










  • It is very likely the problem is within the adapter, can you post the code of your adapter?
    – Aaron
    Nov 22 at 9:59










  • @Piyush I am not sure how to tell which checkbox to setSelected false.
    – cole
    Nov 22 at 10:06










  • @Aaron I updated my question with adapter class added
    – cole
    Nov 22 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 at 10:09













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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;
}









share|improve this question















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 android android-recyclerview android-checkbox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 10:18

























asked Nov 22 at 9:54









cole

1,1112816




1,1112816












  • 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 at 9:56










  • It is very likely the problem is within the adapter, can you post the code of your adapter?
    – Aaron
    Nov 22 at 9:59










  • @Piyush I am not sure how to tell which checkbox to setSelected false.
    – cole
    Nov 22 at 10:06










  • @Aaron I updated my question with adapter class added
    – cole
    Nov 22 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 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 at 9:56










  • It is very likely the problem is within the adapter, can you post the code of your adapter?
    – Aaron
    Nov 22 at 9:59










  • @Piyush I am not sure how to tell which checkbox to setSelected false.
    – cole
    Nov 22 at 10:06










  • @Aaron I updated my question with adapter class added
    – cole
    Nov 22 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 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 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 at 9:56












It is very likely the problem is within the adapter, can you post the code of your adapter?
– Aaron
Nov 22 at 9:59




It is very likely the problem is within the adapter, can you post the code of your adapter?
– Aaron
Nov 22 at 9:59












@Piyush I am not sure how to tell which checkbox to setSelected false.
– cole
Nov 22 at 10:06




@Piyush I am not sure how to tell which checkbox to setSelected false.
– cole
Nov 22 at 10:06












@Aaron I updated my question with adapter class added
– cole
Nov 22 at 10:06




@Aaron I updated my question with adapter class added
– cole
Nov 22 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 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 at 10:09












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










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.






share|improve this answer





















  • Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
    – cole
    Nov 22 at 10:17






  • 1




    Managed to run your code and its working now !! Thanks and enjoy some good points.
    – cole
    Nov 22 at 10:24










  • what didnt work for you to update the answer to help the others :)
    – Hossam Hassan
    Nov 22 at 10:47




















up vote
1
down vote













 @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());
}





share|improve this answer





















    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',
    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
    });


    }
    });














    draft saved

    draft discarded


















    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








    up vote
    1
    down vote



    accepted










    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.






    share|improve this answer





















    • Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
      – cole
      Nov 22 at 10:17






    • 1




      Managed to run your code and its working now !! Thanks and enjoy some good points.
      – cole
      Nov 22 at 10:24










    • what didnt work for you to update the answer to help the others :)
      – Hossam Hassan
      Nov 22 at 10:47

















    up vote
    1
    down vote



    accepted










    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.






    share|improve this answer





















    • Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
      – cole
      Nov 22 at 10:17






    • 1




      Managed to run your code and its working now !! Thanks and enjoy some good points.
      – cole
      Nov 22 at 10:24










    • what didnt work for you to update the answer to help the others :)
      – Hossam Hassan
      Nov 22 at 10:47















    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 at 10:05









    Hossam Hassan

    128115




    128115












    • Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
      – cole
      Nov 22 at 10:17






    • 1




      Managed to run your code and its working now !! Thanks and enjoy some good points.
      – cole
      Nov 22 at 10:24










    • what didnt work for you to update the answer to help the others :)
      – Hossam Hassan
      Nov 22 at 10:47




















    • Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
      – cole
      Nov 22 at 10:17






    • 1




      Managed to run your code and its working now !! Thanks and enjoy some good points.
      – cole
      Nov 22 at 10:24










    • what didnt work for you to update the answer to help the others :)
      – Hossam Hassan
      Nov 22 at 10:47


















    Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
    – cole
    Nov 22 at 10:17




    Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
    – cole
    Nov 22 at 10:17




    1




    1




    Managed to run your code and its working now !! Thanks and enjoy some good points.
    – cole
    Nov 22 at 10:24




    Managed to run your code and its working now !! Thanks and enjoy some good points.
    – cole
    Nov 22 at 10:24












    what didnt work for you to update the answer to help the others :)
    – Hossam Hassan
    Nov 22 at 10:47






    what didnt work for you to update the answer to help the others :)
    – Hossam Hassan
    Nov 22 at 10:47














    up vote
    1
    down vote













     @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());
    }





    share|improve this answer

























      up vote
      1
      down vote













       @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());
      }





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









         @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());
        }





        share|improve this answer












         @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());
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 10:30









        Farman Ali Khan

        299311




        299311






























            draft saved

            draft discarded




















































            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo