2

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;
}
6
  • It's Single item choice selection. You can manage it by declaring position = -1 and check your adapter position while clicking on it. Commented Nov 22, 2018 at 9:56
  • It is very likely the problem is within the adapter, can you post the code of your adapter? Commented Nov 22, 2018 at 9:59
  • @Piyush I am not sure how to tell which checkbox to setSelected false. Commented Nov 22, 2018 at 10:06
  • @Aaron I updated my question with adapter class added Commented Nov 22, 2018 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. Commented Nov 22, 2018 at 10:09

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Hossam. But I could not see it working. I want to deselect any previous ticked checkbox.
Managed to run your code and its working now !! Thanks and enjoy some good points.
what didnt work for you to update the answer to help the others :)
2
 @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());
    }

Comments

2

Complete example

public class ChildAddressAdapter extends 
        RecyclerView.Adapter<ChildAddressAdapter.CartViewHolder> {

private Activity context;

private List<AddressDetail> addressDetailList;

private int selectedPosition = -1;

public ChildAddressAdapter(Activity context, List<AddressDetail> addressDetailList) {
    this.context = context;
    this.addressDetailList = addressDetailList;
}

@NonNull
@Override
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    LayoutInflater inflater = LayoutInflater.from(context);
    View myView = inflater.inflate(R.layout.address_layout, parent, false);
    return new CartViewHolder(myView);
}

@Override
public void onBindViewHolder(@NonNull CartViewHolder holder, int position) {

    holder.adress_checkbox.setOnClickListener(view -> {
        selectedPosition = holder.getAdapterPosition();
        notifyDataSetChanged();
    });

    if (selectedPosition==position){
        holder.adress_checkbox.setChecked(true);
    }
    else {
        holder.adress_checkbox.setChecked(false);
    }


}

@Override
public int getItemCount() {
    return  addressDetailList.size();
}

class CartViewHolder extends RecyclerView.ViewHolder
{
    TextView address_text,address_tag;
    CheckBox adress_checkbox;

    CartViewHolder(View itemView) {
        super(itemView);
        address_text = itemView.findViewById(R.id.address_text);
        address_tag = itemView.findViewById(R.id.address_tag);
        adress_checkbox = itemView.findViewById(R.id.adress_checkbox);
    }
}}

1 Comment

Thank You for answer. Your answer deserve checkmark.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.