Search code examples
androidandroid-recyclerviewandroid-checkbox

How to set check box true if once checked in RecyclerView like flipkart price range


Hello can any one help me how to set checked true checkbox in RecyclerView if user checked it once and apply filter same as flipkart price filter

here is my code :-

    final ResultGetCategory datum = moviesList.get(position);
    holder.label_category_name.setText(datum.getName());
    //checkbox click event handling

    holder.checkbox.setChecked(datum.isSelected());
    holder.checkbox.setTag(moviesList.get(position));

    SharedPreferences prefsCheckBox = getContext().getSharedPreferences(String.valueOf(holder.checkbox.getTag()), Context.MODE_PRIVATE);
    final SharedPreferences.Editor editorCheckbox = prefsCheckBox.edit();

    holder.checkbox.setTag(moviesList.get(position));
    holder.checkbox.setChecked(
            prefsCheckBox.getBoolean(String.valueOf(holder.checkbox.getTag()), false));
    holder.checkbox.setOnCheckedChangeListener(new
                                                       CompoundButton.OnCheckedChangeListener() {
                                                           @Override
                                                           public void onCheckedChanged(CompoundButton buttonView,
                                                                                        boolean isChecked) {
                                                               //add your code here when set checked changed


                                                               editorCheckbox.putBoolean(String.valueOf(holder.checkbox.getTag()),isChecked);
                                                               editorCheckbox.apply();
                                                           }
                                                       });

Solution

  • try it instead of set on click:

        holder.checkbox.setTag(moviesList.get(position));
        holder.checkBox.setChecked(
               prefs.getBoolean(
                  String.valueOf(holder.checkbox.getTag(),false));
        holder.checkBox.setOnCheckedChangeListener(new 
             CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, 
                         boolean isChecked) {
                //add your code here when set checked changed
                editor.putBoolean(
                    String.valueOf(holder.checkbox.getTag()),isChecked);
                editor.apply();
            }
        });
    

    this code work for multiple checkboxes in a ListView each checkbox have a sharedpreferences boolean value and all default is false.

    Your Updated code:

        final ResultGetCategory datum = moviesList.get(position);
        holder.label_category_name.setText(datum.getName());
        //checkbox click event handling
    
        holder.checkbox.setChecked(datum.isSelected());
        holder.checkbox.setTag(moviesList.get(position));
    /*
      //move commented lines to viewholder class or in constructor of 
          adapter
      //exactly these codes and dont change them
        SharedPreferences prefsCheckBox = 
           getContext().getSharedPreferences("checkbox", 
               Context.MODE_PRIVATE);
    final SharedPreferences.Editor editorCheckbox = prefsCheckBox.edit();
    */
    holder.checkbox.setTag(moviesList.get(position));
    holder.checkbox.setChecked(
    
    prefsCheckBox.getBoolean(
            String.valueOf(holder.checkbox.getTag()), false));
    holder.checkbox.setOnCheckedChangeListener(new                                                    
              CompoundButton.OnCheckedChangeListener() {
           @Override
          public void onCheckedChanged(CompoundButton buttonView,                                                                                   
                                         boolean isChecked) {
      //add your code here when set checked changed
    
    
    
       editorCheckbox.putBoolean(
          String.valueOf(holder.checkbox.getTag()),isChecked);
    
            editorCheckbox.apply();
            }
     });
    

    before test this code erase your app from device and install it again Don't forget to change location of prefs initializing because it will happen for every cell and its not true.