0

There are answers of this questions but none of them solved my problem .Below is my code .

  public class QuizAdapter extends RecyclerView.Adapter<QuizAdapter.MyViewHolder> {
Context context;
private List<Quiz_G_S> quiz_g_sList = null;
int selectedPosition = -1;


public QuizAdapter(Context context, List<Quiz_G_S> list) {
    this.context = context;
    this.quiz_g_sList = list;
}


@Override
public QuizAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.quiz_single_row, parent, false);

    return new MyViewHolder(itemView);

}

@Override
public void onBindViewHolder(final QuizAdapter.MyViewHolder holder, int position) {

    final int pos = position;

    holder.answers.setText(quiz_g_sList.get(pos).getMCQ());

    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            quiz_g_sList.get(pos).setChecked(true);

        }
    });

    if (quiz_g_sList.get(pos).isChecked()) {
        holder.checkBox.setChecked(true);
        notifyDataSetChanged();

    } else {
        holder.checkBox.setChecked(false);

    }





}


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

public class MyViewHolder extends RecyclerView.ViewHolder {

    TextView answers;
    CheckBox checkBox;

    public MyViewHolder(View v) {
        super(v);

        answers = (TextView) v.findViewById(R.id.quiz_adap_ans);
        checkBox = (CheckBox) v.findViewById(R.id.quiz_adap_check);

    }
}

Above is my adapter code.

This is my Quiz_G_S class

public class Quiz_G_S {


String MCQ;
int Rank;
boolean checked;

public boolean isChecked() {
    return checked;
}

public void setChecked(boolean checked) {
    this.checked = checked;
}

public String getMCQ() {
    return MCQ;
}

public void setMCQ(String mcq) {
    this.MCQ = mcq;
}

public int getRank() {
    return Rank;
}

public void setRank(int rank) {
    this.Rank = rank;
}

}

What i want to do is if i check a checkbox then all checkboxes will set unchecked except the one I checked There is an error on notifyDataSetChanged. I guess the logic is right but i don't know how to refresh checkbox state.The only thing I know is notifyDataSetChanged is there any other method to refresh or is there any workaround for this problem

dhami_ji
  • 144
  • 12

4 Answers4

1

try below code:

  1. Define one more Object in your Quiz_G_S Class boolean isChecked

  2. Get the position of checked CheckBox

    selectedPosition = holder.getAdapterPosition();

  3. Reset all other objects of List like below:

    for (int i=0; i<list.size ; i++) 
    {  
      if (selectedPosition==i)  
         list.get(i).setIsChecked(true); 
      else     
         list.get(i).setIsChecked(false);
    }
    
  4. call QuizAdapter.this.notifyDataSetChanged();.

  5. In last, check selected check box like below:

    if (list.get(position).getIsChecked()) 
    {
       holder.checkBox.setChecked(true);
    } else {
      holder.checkBox.setChecked(false);
    }
    
nivesh shastri
  • 422
  • 2
  • 13
0

Instead of

 selectedPosition = holder.getAdapterPosition();

maintain a flag inside your object class which will be set on Check of particular checkbox suppose "checked" is your flag that you have created in your object class then

on check change listener set it to true

if (isChecked) {
    holder.checkBox.setChecked(true);

} else {
    holder.checkBox.setChecked(false);

}

EDIT

 if (quiz_g_sList.get(pos).isChecked) {
        holder.checkBox.setChecked(true);

    } else {
        holder.checkBox.setChecked(false);

    }

UPDATE

change your object class to this

 public class Quiz_G_S {

        String MCQ;
        int Rank;
        boolean checked;

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public String getMCQ() {
            return MCQ;
        }

        public void setMCQ(String mcq) {
            this.MCQ = mcq;
        }

        public int getRank() {
            return Rank;
        }

        public void setRank(int rank) {
            this.Rank = rank;
        }
    }
yogesh lokhande
  • 1,049
  • 1
  • 10
  • 20
0

I have implemented like this, where i got output.

if(view == null) {

        view = inflater.inflate(R.layout.todo_checkbox_adapter_layout, parent, false);
        holder = new ViewHolder();
        holder.todoItemTextView = (TextView) view.findViewById(R.id.item_tv);
        holder.checkedTextView = (CheckBox) view.findViewById(R.id._item_checkbox);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    holder.todoItemTextView.setTag("" + position);
    holder.checkedTextView.setChecked(displayModel.isFinished());

    holder.checkedTextView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            try {
                String tag = (String) holder.todoItemTextView.getTag();
                int pos = Integer.parseInt(tag);
                todoItemBeanList.get(pos).setFinished(isChecked);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
    });
Karthik
  • 1,155
  • 1
  • 13
  • 22
0

What do you have to do:

  1. Pass checked position to Activity/Fragment/Presenter (Implement click/checkChanged listener). Examples can be found here: RecyclerView onClick
  2. Save position of checked item into a variable (say int selectedPosition).
  3. Then update previously checked item on each click/check:

if (position != selectedPosition) { // user selected another item so previous one should be updated adapter.notifyItemChanged(selectedPosition); selectedPosition = position; }

Leo Droidcoder
  • 11,898
  • 3
  • 55
  • 48