简体   繁体   中英

How to use Shared Preferences in adapter?

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.

Does setting

checkBox.setChecked(isChecked);   

Not make it work as you want?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM