0

I am creating an application that uses the concept of teams for the users. So I used this solution to separate the users into teams in my recyclerView, now I want to be able to click on the checkbox of the team header and select all of its children.

But the problem is that the checkBox for each item is being handled separately from the checkbox of the header. So I tried to do the proper changes but I wasn't able to. I can get all of the items from the selected team but I would also like to check the boxes from them too.

This is my adapter:

public class GroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    List<ListItem> consolidatedList = new ArrayList<>();

    public GroupAdapter(Context context, List<ListItem> consolidatedList) {
        this.consolidatedList = consolidatedList;
        this.mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        switch (viewType) {

            case ListItem.TYPE_GENERAL:
                View v1 = inflater.inflate(R.layout.adapter_teams, parent,
                        false);
                viewHolder = new GeneralViewHolder(v1);
                break;

            case ListItem.TYPE_TEAM:
                View v2 = inflater.inflate(R.layout.adapter_team_header, parent, false);
                viewHolder = new DateViewHolder(v2);
                break;
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {

        switch (viewHolder.getItemViewType()) {

            case ListItem.TYPE_GENERAL:

                GeneralItem generalItem   = (GeneralItem) consolidatedList.get(position);
                final GeneralViewHolder generalViewHolder = (GeneralViewHolder) viewHolder;

                generalViewHolder.name.setText(generalItem.getPojoOfJsonArray().getName());
                generalViewHolder.description.setText(generalItem.getPojoOfJsonArray().getCompany());
                if(generalItem.getPojoOfJsonArray().getImageURL() != null && !generalItem.getPojoOfJsonArray().getImageURL().isEmpty()) {
                    Picasso.with(mContext).load(generalItem.getPojoOfJsonArray().getImageURL()).into(generalViewHolder.profile_image);
                }

                generalViewHolder.check_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){

                            if (ActivityTeams.user_type == 1 && !ActivityTeams.newList.contains(((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray()) && !ActivityTeams.listOwners.contains(((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray())) {
                                UserProject.addUserUniqueTag(ActivityTeams.newList, ((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray(), UserTag.COLLABORATOR);
                            } else if(ActivityTeams.user_type == 2 && !ActivityTeams.listOwners.contains(((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray()) && !ActivityTeams.newList.contains(((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray())) {
                                UserProject.addUserUniqueTag(ActivityTeams.listOwners, ((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray(), UserTag.OWNER);
                            }
                        }else {
                            if(ActivityTeams.user_type == 1) {
                                ActivityTeams.newList.remove(((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray());
                                UserProject.addUserUniqueTag(ActivityTeams.removedColab, ((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray(), UserTag.COLLABORATOR);
                            } else if (ActivityTeams.user_type == 2) {
                                ActivityTeams.listOwners.remove(((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray());
                                UserProject.addUserUniqueTag(ActivityTeams.removedOwner, ((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray(), UserTag.OWNER);
                            }
                        }
                    }
                });


                if (ActivityTeams.user_type == 1) {
                    generalViewHolder.check_item.setChecked(ActivityTeams.newList.contains(((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray()));
                } else if (ActivityTeams.user_type == 2) {
                    generalViewHolder.check_item.setChecked(ActivityTeams.listOwners.contains(((GeneralItem) consolidatedList.get(position)).getPojoOfJsonArray()));
                }


                break;

            case ListItem.TYPE_TEAM:
                TeamItem dateItem = (TeamItem) consolidatedList.get(position);
                DateViewHolder dateViewHolder = (DateViewHolder) viewHolder;

                if (dateItem.getTeam() == null) {
                    dateViewHolder.txtTitle.setText(mContext.getString(R.string.team_no_team));
                } else {

                    for (int i = 0; i < ActivityTeams.listTeams.size(); i ++) {
                        if (dateItem.getTeam().equals(ActivityTeams.listTeams.get(i).get_id())) {
                            dateViewHolder.txtTitle.setText(ActivityTeams.listTeams.get(i).getName());
                        }
                    }

                }

                dateViewHolder.check_group.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){

                            System.out.println(((TeamItem) consolidatedList.get(position)).getTeam());
                            ArrayList<UserProject> group_list = new ArrayList<>();
                            for (int i = 0; i< ActivityTeams.listUsers.size(); i++) {
                                if (ActivityTeams.listUsers.get(i).getTeamId().equals(((TeamItem) consolidatedList.get(position)).getTeam())) {
                                    group_list.add(ActivityTeams.listUsers.get(i));
                                }
                            }

                        }else {

                        }
                    }
                });

                break;
        }
    }





    // ViewHolder for date row item
    class DateViewHolder extends RecyclerView.ViewHolder {
        protected TextView txtTitle;
        protected TextView count;
        protected CheckBox check_group;

        public DateViewHolder(View v) {
            super(v);
            this.txtTitle = (TextView) v.findViewById(R.id.title);
            this.count = (TextView) v.findViewById(R.id.count);
            this.check_group = (CheckBox) v.findViewById(R.id.check);

        }
    }

    // View holder for general row item
    class GeneralViewHolder extends RecyclerView.ViewHolder {
        public TextView name, description;
        public CircleImageView profile_image;
        public CheckBox check_item;

        public GeneralViewHolder(View v) {
            super(v);
            this.name = (TextView) v.findViewById(R.id.name);
            this.description = (TextView) v.findViewById(R.id.description);
            this.profile_image = (CircleImageView) v.findViewById(R.id.profile_image);
            this.check_item = (CheckBox) v.findViewById(R.id.check);

        }
    }

    @Override
    public int getItemViewType(int position) {
        return consolidatedList.get(position).getType();
    }

    @Override
    public int getItemCount() {
        return consolidatedList != null ? consolidatedList.size() : 0;
    }

} 
halfer
  • 19,824
  • 17
  • 99
  • 186
Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50
  • Possible duplicate of [How to select all items which listed in recyclerview?](https://stackoverflow.com/questions/45578817/how-to-select-all-items-which-listed-in-recyclerview) – RestingRobot Sep 18 '17 at 21:16
  • Thank you for the reply. If you check my adapter you will see that there are two "cases" one for the header and the other for the list of items. Since they are separated I wasn't able to check the item inside the 'case' of the header. – Marcos Guimaraes Sep 18 '17 at 21:21
  • You have 2 view holders, but they are managed by the same adapter. The solution should still be valid. – RestingRobot Sep 18 '17 at 21:23
  • You will have create a super class, (a class that `GeneralItem` and `TeamItem` both inherit from), but that shouldn't be hard. – RestingRobot Sep 18 '17 at 21:27
  • But how can I deal with one viewHolder inside of the scope of another? – Marcos Guimaraes Sep 18 '17 at 21:28
  • You don't deal with the viewHolder, you deal with the data. You should be modifying `consolidatedList` items. Your check box should be driven by data not by accessing a static variable in your activity. Then when you call `notifyDataSetChanged`, your views will refresh and will show the checked boxes. You should consider revising your logic. – RestingRobot Sep 18 '17 at 21:31
  • Do you have any example that does something similar to what you're suggesting? So I can see how it's done and be able to adapt to my case. I'm pretty new at this so I am still getting used to it. – Marcos Guimaraes Sep 18 '17 at 21:32
  • The answer that I linked has a good basis for you to start, you should make `List selectedList = new ArrayList<>();` another list in your adapter. Then in the `onCheckedChangedListener`, add/remove that item from the `selectedList`. – RestingRobot Sep 18 '17 at 21:38

0 Answers0