Removing item from list when clicking on an item in recyclerview

by kippi   Last Updated January 11, 2019 11:26 AM

I'm building a project app where you can add users to a task. I have a recycler view with all my users. When you tap on a user that user is added to an array list, it all works fine. The problem I got is that you can't deselect a user. When tapping on a user that already has been selected that user should be removed from the array list. How do you do this?

My Adapter:

public class AddPeopleToTaskAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {

public ArrayList<String> peopleToAddToTask = new ArrayList<String>();

public List<PeopleInProject> peopleInProjects;
public Context context;

public AddPeopleToTaskAdapter(Context context, List<PeopleInProject> peopleInProjects) {
    this.peopleInProjects = peopleInProjects;
    this.context = context;
}


@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.add_people_to_task_recyclerview, viewGroup, false);
    return new RecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final RecyclerViewHolder viewHolder, final int i) {

    final PeopleInProject peopleToAdd = peopleInProjects.get(i);

    viewHolder.addedIcon.setVisibility(View.INVISIBLE);
    viewHolder.nameLbl.setText(peopleToAdd.getUserName());
    viewHolder.setItemClickListenerPeopleToAdd(new ItemClickListenerPeopleToAdd() {
        @Override
        public void onClick(View view, int position) {
            Toast.makeText(context, "You added " + peopleToAdd.getEmail(), Toast.LENGTH_SHORT).show();
            peopleToAddToTask.add(peopleToAdd.getUserId().toString());
            viewHolder.addedIcon.setVisibility(View.VISIBLE);
            viewHolder.addIcon.setVisibility(View.INVISIBLE);

            if (peopleToAddToTask.contains(peopleToAdd.getUserId().toString())) {

            }
        }
    });


}

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

I also shows an image when a user has been added (addedIcon). Which shows correctly. When you deselect a user that image should be replaced with addIcon.

I tried this but it didn't work.

@Override
public void onBindViewHolder(@NonNull final RecyclerViewHolder viewHolder, final int i) {

    final PeopleInProject peopleToAdd = peopleInProjects.get(i);

    viewHolder.addedIcon.setVisibility(View.INVISIBLE);
    viewHolder.nameLbl.setText(peopleToAdd.getUserName());
    viewHolder.setItemClickListenerPeopleToAdd(new ItemClickListenerPeopleToAdd() {
        @Override
        public void onClick(View view, int position) {
            Toast.makeText(context, "You added " + peopleToAdd.getEmail(), Toast.LENGTH_SHORT).show();
            peopleToAddToTask.add(peopleToAdd.getUserId().toString());
            viewHolder.addedIcon.setVisibility(View.VISIBLE);
            viewHolder.addIcon.setVisibility(View.INVISIBLE);

            if (peopleToAddToTask.contains(peopleToAdd.getUserId().toString())) {

                peopleToAddToTask.remove(i);
                viewHolder.addedIcon.setVisibility(View.INVISIBLE);
                viewHolder.addIcon.setVisibility(View.VISIBLE);

            }
        }
    });


}

I appreciate all help. Tnx!



Answers 1


I think you should first go through your array and check for containing the user

for(int i = 0; i < peopleToAddToTask.size(); i++){
   if (peopleToAddToTask.contains(peopleToAdd.getUserId().toString())) {
      peopleToAddToTask.remove(i);
      viewHolder.addedIcon.setVisibility(View.INVISIBLE);
      viewHolder.addIcon.setVisibility(View.VISIBLE);
   }
}
Asset Bekbossynov
Asset Bekbossynov
January 11, 2019 11:25 AM

Related Questions


Updated February 04, 2018 07:26 AM

Updated December 29, 2017 17:26 PM

Updated February 12, 2018 19:26 PM

Updated July 01, 2018 02:26 AM

Updated May 16, 2016 09:11 AM