By kippi


2019-01-11 11:00:21 8 Comments

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!

1 comments

@Asset Bekbossynov 2019-01-11 11:25:56

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

@kippi 2019-01-11 11:34:28

It is not working. For some reason everyone is removed from the array. Any ideas why?

@Asset Bekbossynov 2019-01-11 11:36:15

Try to check whether your peopleToAdd.getUserId() is user that should be removed

Related Questions

Sponsored Content

23 Answered Questions

2 Answered Questions

Android AlarmManager for periodical sensor reading

37 Answered Questions

[SOLVED] How to add dividers and spaces between items in RecyclerView?

1 Answered Questions

1 Answered Questions

3 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] Scrolling lagged after applying the typeface in the Recycler view items

  • 2016-02-26 11:56:13
  • leo
  • 268 View
  • 2 Score
  • 1 Answer
  • Tags:   java android

4 Answered Questions

Sponsored Content