我在此行遇到麻烦:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
我遇到的问题是(this)
有一条红线。但是其余的代码是可以的。这可能是什么问题?
这是其余的代码:
public static class ShowDataViewHolder extends RecyclerView.ViewHolder {
private final CheckedTextView image_title;
private final CheckBox chk;
public ShowDataViewHolder(final View itemView) {
super(itemView);
image_title = (CheckedTextView) itemView.findViewById(R.id.fetch_image_title);
chk = (CheckBox)itemView.findViewById(R.id.checkBox);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences();
final SharedPreferences.Editor editor = preferences.edit();
if (preferences.contains("checked") && preferences.getBoolean("checked", false) == true){
chk.setChecked(true);
}else{
chk.setChecked(false);
}
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (chk.isChecked()){
editor.putBoolean("checked", true);
editor.apply();
}else{
editor.putBoolean("checked", false);
editor.apply();
}
}
});
}
private void Image_Title(String title) {
image_title.setText(title);
}
此类位于我的MainActivity内部。