2

I created a Checkbox, however, whenever I click a few Items random checkboxes become clicked. I have a recycler view, and I have an adapter class as well. Here is my CheckBox method can anyone tell me the problem?

public void CheckBox(View view) {
    final CheckBox checkBox = (CheckBox)view;
    if (checkBox.isChecked()) {

        System.out.println("SET TO CHECKED");
        //Input instance of selected course(CHECKED)
        // TODO: 2/5/16 Need to pass in Persisted ID
        mVolleySingleton = VolleySingleton.getInstance();
        mRequestQueue = mVolleySingleton.getRequestQueue();
        CharSequence ID_PUT_COURSES = ((TextView) ((RelativeLayout) view.getParent()).getChildAt(1)).getText();
        System.out.println(PUT);

        String URL_PUT_COURSES = "URL"+ID_PUT+"\n";
        System.out.print(PUT);
        StringRequest UrlPut = new StringRequest(Request.Method.PUT, URL_PUT, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println(response + "reponse");

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("************Answer" + error + "error");
            }
        });
        mRequestQueue.add(UrlPutCourses);
        System.out.println("Added");
    }

else{
        System.out.println("SET TO UNCHECKED");
        //delete instance of selected course(UNCHECKED)
        mVolleySingleton = VolleySingleton.getInstance();
        mRequestQueue = mVolleySingleton.getRequestQueue();
        // TODO: 2/5/16 Need to pass in Persisted ID
        CharSequence DELETE = ((TextView) ((RelativeLayout) view.getParent()).getChildAt(1)).getText();
        System.out.print(ID_PUT_COURSES);
        String UR_DELETE = "URL"+ DELETE;
        StringRequest UrlDeleteCourses = new StringRequest(Request.Method.DELETE, UR_DELETE, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println(response + "reponse");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("************Answer" + error + "error");
            }
        });
        mRequestQueue.add(UR_DELETE);
        System.out.println("Deleted");
    }

}



ublic class AdapterSearch extends RecyclerView.Adapter<AdapterSearch.ViewSearch>{
private LayoutInflater mLayoutInflater;
private ArrayList<Search> ListSearch=new ArrayList<>();
public AdapterSearch(Context context){

    mLayoutInflater=LayoutInflater.from(context);

}
public void setListSearch(ArrayList<Search> ListSearch){
    this.ListSearch=ListSearch;
    notifyItemRangeChanged(0,ListSearch.size());
}

@Override
public ViewSearch onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= mLayoutInflater.inflate(R.layout.custom_search,(ViewGroup)null);
    ViewSearch holder=new ViewSearch(view);
    return holder;
}

@Override
public void onBindViewHolder(ViewSearch holder, int position) {
    Search currentSearch=ListSearch.get(position);
    holder.mSearchText.setText(currentSearch.getMtitle());
    holder.mAnswerPointsSearch.setText(currentSearch.getMkey());
    holder.mSearchId.setText(currentSearch.getMid());
    holder.mCourseId.setText(currentSearch.getCourseId());
}

@Override
public int getItemCount() {
    return ListSearch.size();
}
public void setFilter(ArrayList<Search> Search) {
    ListSearch = new ArrayList<>();
    ListSearch.addAll(Search);
    notifyDataSetChanged();
}

static class ViewSearch extends RecyclerView.ViewHolder{
    private TextView mSearchText;
    private TextView mAnswerPointsSearch;
    private TextView mSearchId;
    private TextView mCourseId;



    public ViewSearch (View view){
        super(view);
        mSearchText=(TextView)itemView.findViewById(R.id.SearchText);
        mAnswerPointsSearch=(TextView)itemView.findViewById(R.id.AnswerPointsSearch);
        mSearchId=(TextView)itemView.findViewById(R.id.SearchId);
        mCourseId=(TextView)itemView.findViewById(R.id.CourseTextView);



    }
}

}

Arash GM
  • 10,316
  • 6
  • 58
  • 76
Eli
  • 827
  • 1
  • 7
  • 21
  • 1
    problem is inside your adapter class where it recycle views – Arash GM Feb 08 '16 at 09:50
  • And what is the problem? – Eli Feb 08 '16 at 09:52
  • 1
    the code you've posted doesn't show that part, you have to persist chkbox states on the adapter and whenever onBindViewHolder get called re-set checkbox states again.. – Arash GM Feb 08 '16 at 09:55
  • I posted my Adapter Class can you please explain in further detail...? – Eli Feb 08 '16 at 10:00
  • 1
    you didn't set your chbox states so look out this thread to find out how you can do that . good luck : http://stackoverflow.com/questions/32427889/checkbox-in-recyclerview-keeps-on-checking-different-items – Arash GM Feb 08 '16 at 10:02

1 Answers1

0

Better late then never I'd say, it might help other people stumbling upon this issue later, so here goes.

This might not be the best solution ever, but it should work.

In your Search class, you have some variables like title and key. Add the boolean isChecked to it, and make it false by default. Also create the matching getter and setter. (I'm using isChecked() & setChecked() in this example)

In your onBindViewHolder() method, add the following lines:

CheckBox checkBox = (CheckBox) itemView.findViewById(R.id.CheckBoxID);
// Instantiating the checkbox

holder.checkBox.setChecked(currentSearch.isChecked());
// Every time the holder (one list item) is shown, this gets called. 
// It sets the checked state based on what's stored in currentSearch.

checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkBox.isChecked()) {
                currentSearch.setChecked(true)
            } else {
                currentSearch.setChecked(false);
            }
        });
}
// This listens for clicks on the checkbox, and changes the checked state of
// the boolean in currentSearch to the correct state based on 
// what it already was on.

Please be aware that I haven't worked with checkboxes before, so some things (like setting the checked state) might be done slightly differently from what I've described above, but this should be a working base.

You also probably want the checkboxes to only persevere until you click a button or something like that, and then have them reset. You might want to add a simple for loop for that, something like:

public void resetAllCheckBoxes() {
    for (Search s : ListSearch) {
        s.setChecked(false);
    }
}
Timmiej93
  • 1,328
  • 1
  • 16
  • 35