Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从共享首选项中记住上一个数组_Java_Android_Arrays_Sharedpreferences - Fatal编程技术网

Java 从共享首选项中记住上一个数组

Java 从共享首选项中记住上一个数组,java,android,arrays,sharedpreferences,Java,Android,Arrays,Sharedpreferences,只是一个关于SharedReference和int数组的快速问题。这是我的一些代码的相关位。我想发生的是,如果用户做了一些模糊的事情,因为它是不相关的,那么数组会在那个位置保持2。相反,如果每次我关闭应用程序或更改活动时,数组都会变回“全一”而不是“两个”。这可能是一个微不足道的问题,如果是的话,很抱歉 public class SecondActivity extends AppCompatActivity { int[] list = { 1, 1, 1, 1, 1, 1 };

只是一个关于SharedReference和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;
    }

}

您可以尝试两种解决方案

解决方案1

将int数组设为静态,并引用活动

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 
     }

}

我希望它能帮助你

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

或在onCreate中执行以下操作:

list = isCorrect;

我认为它应该会起作用。

嗨,非常感谢你的回复!第一个解决方案几乎奏效,但当我关闭应用程序并选择另一个框时,仍然被覆盖。我的意思是,当我启动应用程序时,数组是{1,1,1,1,1},我让1和3变成2,如下{1,2,1,2,1,1},然后我会关闭应用程序,再次打开,让0变成2,然后它会显示{2,1,1,1,1}。第二个解决方案,android studio说它们是不兼容的类型?如果您对其中任何一项有任何提示,我将不胜感激。谢谢你,我没有什么不理解的。看看这篇文章,最后一个我无法解决的问题。从头开始时,变量isCorrect等于[]。所以它崩溃了,因为里面什么都没有。我如何在不破坏其余代码的情况下初始化它?谢谢我也应该说,这发生在我也做了你的第一个建议。我还试图使列表保持静态,但仍然不起作用。非常感谢您的帮助。