【问题标题】:Remembering the previous array from shared preference从共享首选项中记住前一个数组
【发布时间】:2018-05-28 13:38:36
【问题描述】:

关于 sharedPreferences 和 int 数组的简单问题。这是我的一些代码(相关位)。而我想要发生的是,如果用户做了某事(保持模糊,因为它不相关),那么数组将 2 保持在该位置。相反,如果每次我关闭应用程序或更改活动,数组就会恢复为没有二的全为一。这可能是一个小问题,如果是,请见谅。

public class SecondActivity extends AppCompatActivity {

    int[] list = { 1, 1, 1, 1, 1, 1 };

public void startAQuestion(View view){ 

    checkAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { 

                if(editText2.getText().toString().equals(mAnswer)) {

                    list[tappedQuestionmark]=2; 

                    storeIntArray("updateList", list);

                    Log.i("The array is ", Arrays.toString(list));
                }    
            }
            });
        }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        int[] isCorrect = getFromPrefs("updateList");

        Log.i("is Correct is ", Arrays.toString(isCorrect));

    }

    public void storeIntArray(String name, int[] array) {
        SharedPreferences.Editor edit = this
                .getSharedPreferences("com.example.sid.sharedpreferencedemo", Context.MODE_PRIVATE).edit();
        edit.putInt("Count_" + name, array.length).commit();
        int count = 0;
        for (int i : array) {
            edit.putInt("IntValue_" + name + count++, i).commit();
        }
        edit.commit();
    }

    public int[] getFromPrefs(String name) {
        int[] ret;
        SharedPreferences prefs = this.getSharedPreferences("com.example.sid.sharedpreferencedemo",
                Context.MODE_PRIVATE);
        int count = prefs.getInt("Count_" + name, 0);
        ret = new int[count];
        for (int i = 0; i < count; i++) {
            ret[i] = prefs.getInt("IntValue_" + name + i, i);
        }
        return ret;
    }

}

【问题讨论】:

    标签: java android arrays sharedpreferences


    【解决方案1】:

    每次打开应用程序时,您的 list 变量都会被初始化为所有变量。您需要将共享首选项中的列表加载到 list 变量中,而不是 isCorrect 数组中,因为当您通过单击按钮更新存储在共享首选项中的列表时,您可以从那里获取值。

    或者在 onCreate 中做:

    list = isCorrect;
    

    我认为它应该可以工作。

    【讨论】:

    • 最后一个我无法解决的问题。从头开始时,变量 isCorrect 等于 []。所以它崩溃了,因为那里什么都没有。我如何在不破坏其余代码的情况下初始化它?谢谢!
    • 我还应该说,当我也做了你的第一个建议时,就会发生这种情况。我还尝试将列表设为静态,但它仍然无法正常工作。非常感谢任何帮助。
    【解决方案2】:

    您可以尝试两种解决方案。

    解决方案 1

    将你的 int 数组设为静态,并参考 Activity

    public class SecondActivity extends AppCompatActivity {
    
       public static int[] list = { 1, 1, 1, 1, 1, 1 };
    
    public void startAQuestion(View view){ 
    .
    .
    

    如何使用

    SecondActivity.list[tappedQuestionmark]=2; 
    

    解决方案 2 使用 savedInstance 传递你的数组

    public void onSaveInstanceState(Bundle outState){
        outState.putSerializable("list ", list);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         list = savedInstanceState.getSerializable("list ");
    
         if(list  != null)
         {
              //Do something with list 
         }
    
    }
    

    希望对你有帮助!

    【讨论】:

    • 您好,非常感谢您的回复!第一个解决方案几乎有效,但当我关闭应用程序并选择另一个框时仍然被覆盖。我的意思是当我启动应用程序时,数组是 {1,1,1,1,1,1} 我让 1 和 3 转到 2 如下 {1,2,1,2,1,1} 然后我将关闭应用程序并再次打开并使 0 变为 2,然后它将显示 {2,1,1,1,1,1}。第二种解决方案,android studio 说它们是不兼容的类型?如果您对其中任何一个有任何提示,我将不胜感激。谢谢
    • 对不起,我没有发现。所以看看这个帖子stackoverflow.com/questions/7175880/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2011-12-25
    • 2011-08-07
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    相关资源
    最近更新 更多