0
class SearchViewHolder extends RecyclerView.ViewHolder{

    TextView studentName, studentMatrik, studentPhone, studentAddress, studentStatus, studentMail, studentCon, studentId;
    CheckBox checkBox;
    Button buttonMap;
    DatabaseReference databaseReference;
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    boolean isChecked = sharedPreferences.getBoolean("checked", false);
    public SearchViewHolder(View itemView) {
        super(itemView);
        studentName = (TextView) itemView.findViewById(R.id.studentName);
        studentMatrik = (TextView) itemView.findViewById(R.id.studentMatrik);
        studentPhone = (TextView) itemView.findViewById(R.id.studentPhone);
        studentAddress = (TextView) itemView.findViewById(R.id.studentAddress);
        studentStatus = (TextView) itemView.findViewById(R.id.studentStatus);
        studentMail = (TextView) itemView.findViewById(R.id.studentMail);
        studentCon = (TextView) itemView.findViewById(R.id.studentCon);
        studentId = (TextView) itemView.findViewById(R.id.studentId);
        checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
        buttonMap = (Button) itemView.findViewById(R.id.buttonMap);
        buttonMap.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MapsViewActivity.class);
                intent.putExtra("Latitude",studentMail.getText().toString());
                intent.putExtra("Longitude",studentCon.getText().toString());
                intent.putExtra("Address",studentAddress.getText().toString());
                context.startActivity(intent);
            }
        });
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    editor.putBoolean("checked", isChecked);
                    editor.commit();
                    String id = studentId.getText().toString();
                    databaseReference = FirebaseDatabase.getInstance().getReference("student");
                    String name = "Check";
                    databaseReference.child(id).child("stat").setValue(name);
                    notifyDataSetChanged();
                } else {
                    String id = studentId.getText().toString();
                    databaseReference = FirebaseDatabase.getInstance().getReference("student");
                    String name = "Not Check";
                    databaseReference.child(id).child("stat").setValue(name);
                    notifyDataSetChanged();
                }
            }
        });
    }

How can I use shared preference to save the checkbox value in adapter? I've try doing it though but still wrong... If I have checked the checkbox, I want the checkbox still be checked when I quit and open back the app, and it will do the same when the checkbox is unchecked.

veen259
  • 33
  • 9
  • you have to make hashmap which stores checkbox value and id which helps in add or delete and save it to the shared preference if you want – Farhana Naaz Ansari Feb 06 '18 at 05:17
  • First, you need to know how to store/read the value on sharedPreference. This will help to understand: https://stackoverflow.com/a/3624358/5167909 – Faysal Ahmed Feb 06 '18 at 05:35

2 Answers2

0

Does setting

checkBox.setChecked(isChecked);   

Not make it work as you want?

Shannon Birch
  • 108
  • 1
  • 9
0

On Check event save the boolean data to SharedPreferences with the id of the list item as key. Then add some code to ViewHolder to check in SharedPreference if an item should be checked before displaying it. This way, the ViewHolder can check if a CheckBox has checked value in SharedPreference every time it is displayed

josh_gom3z
  • 294
  • 1
  • 13