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.