RecyclerView 中的 SharedPreferences

SharedPreferences inside of a RecyclerView

我创建了一个 activity,我在其中实现了一个回收器视图适配器。我还创建了第二个 activity,它应该向 SharedPreferences 添加一个值。之后,我想根据 SharedPreferences 的值更改 textView 的文本。但是当我尝试设置它不起作用的文本时,它甚至没有添加 SharedPreferences 的默认值。

activity 中保存点击数据的代码:

public void Done(View view){

        SharedPreferences mPreferences = 
        mContext.getSharedPreferences("Checker",MODE_PRIVATE);
        SharedPreferences.Editor mEditor = mPreferences.edit();

        mEditor.putString("Day1", "complete");
        mEditor.apply();

        //mRecyclerView.setAdapter(fitnessAdapter);

        fitnessAdapter.notifyDataSetChanged();

回收器视图适配器中的代码:(只是我使用代码的 ViewHolder)

public class MultiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private OnNoteListener mOnNoteListener;
    private Context context;

    public MultiAdapter(List<myObject> Parts, OnNoteListener onNoteListener, Context context) {
        this.items = Parts;
        this.mOnNoteListener = onNoteListener;
        this.context = context;}
     private Context context;

public class MainViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView segment;
        OnNoteListener mOnNoteListener;

        private MainViewHolder( View itemView, OnNoteListener onNoteListener, Context context) {
            super(itemView);
            segment = itemView.findViewById(R.id.segment_shredmas);

            SharedPreferences mPreferences = context.getSharedPreferences("Checker",MODE_PRIVATE);
            String daySelector = mPreferences.getString("Day1","Nothing");

            segment.setText("Day "+ daySelector);

            this.mOnNoteListener = onNoteListener;
            itemView.setOnClickListener(this);
        }
        @Override
        public void onClick(View v) {
            mOnNoteListener.onNoteClick(getAdapterPosition());
        }
    }
 @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.segment_shredmas, parent, false);
            return new ShredmasViewHolder(view, mOnNoteListener,[position] 1, [context]context);

PS : 我尝试了 SharedPreferences 的两种方式。但是因为旧的是最后一个,所以我就这样留下了代码

错误信息:

E/MemoryLeakMonitorManager: MemoryLeakMonitor.jar is not exist!
E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@95326f7
E/: [ZeroHung]zrhung_get_config: Get config failed for wp[0x0102]
W/ZrHung.AppEyeUiProbe: Failed to get config from zrhung
Process: com.example....., PID: 15963
    java.lang.RuntimeException: Unable to pause activity {com.example...../com.example.....MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.MediaPlayer.getCurrentPosition()' on a null object reference
at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4742)
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4691)
        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4626)
        at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:45)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.MediaPlayer.getCurrentPosition()' on a null object reference
        at com.example.....MainActivity.onPause(MainActivity.java:131)
        at android.app.Activity.performPause(Activity.java:7663)
        at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1536)
        at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4726)
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4691) 
        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4626) 
        at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:45) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) 
        at android.os.Handler.dispatchMessage(Handler.java:112) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 
E/: [ZeroHung]zrhung_get_config: Get config failed for wp[0x0008]

编辑:所以我跟随它的轨道,出于某种原因它把我带到了另一个 activity 的媒体播放器,但是当我删除它并重新运行应用程序时,另一个 NullPointer PopedUp:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference so the problem is in the line

所以问题出在行 SharedPreferences mPreferences = mContext.getSharedPreferences("Checker",MODE_PRIVATE);

我知道 mContext 是带有 nullException 的那个,但是当我不在代码中使用 Context 时,它会将它保存到不同的 SharedPrefences 中。当我想在 recyclerView 中加载一些 SharedPreferences 时,我必须像在上面显示的代码中那样引用上下文,这样它就会在组中。

您使用获取和设置 sharef 首选项值的密钥不相同,因此您在 Done 方法中更改了错误的文本。

mEditor.putString("Day1", "complete"); String daySelector = mPreferences.getString("ShredmasD1","Nothing");

使用 mEditor.commit() 而不是 mEditor.apply() 来保存您的 SharedPreferences。