在android缓存中管理数据?

在android缓存中管理数据?,android,caching,Android,Caching,我正试图在未来3周内完成我的应用程序的“主干”,然而,我遇到的几个障碍之一是保存数据。我已经了解了如何在内部保存数据,但是我所能找到的关于在应用缓存目录中读取和写入多行文件的教程非常有限 基本上,我要做的是保存存储在片段中的值。当用户单击按钮并更改文本以匹配页码时,此片段将重置其所有值。(包含各种值的多个副本。)但是,我会使用多个片段,我认为只使用一个片段来最小化所需的存储空间是有益的 我只花了点时间写文件,创建了两个方法来管理它,然后点击一个按钮就可以调用它们。一个创建这些文件,另一个写入。不

我正试图在未来3周内完成我的应用程序的“主干”,然而,我遇到的几个障碍之一是保存数据。我已经了解了如何在内部保存数据,但是我所能找到的关于在应用缓存目录中读取和写入多行文件的教程非常有限

基本上,我要做的是保存存储在片段中的值。当用户单击按钮并更改文本以匹配页码时,此片段将重置其所有值。(包含各种值的多个副本。)但是,我会使用多个片段,我认为只使用一个片段来最小化所需的存储空间是有益的

我只花了点时间写文件,创建了两个方法来管理它,然后点击一个按钮就可以调用它们。一个创建这些文件,另一个写入。不幸的是,我没有使用adb的经验,只能发现文件是创建的,但不知道它们是否被正确写入。有没有可能有人会对此进行审查,并可能帮助重新阅读文件?非常感谢你的帮助

这两种方法(警告:前面有大量行):

public void createEmptyFiles() {
    try {
        outputTempExerciseFileE1 = File.createTempFile("temp_exercise_1",
                ".txt", outputTempExerciseDir);
        outputTempExerciseFileE2 = File.createTempFile("temp_exercise_2",
                ".txt", outputTempExerciseDir);
        outputTempExerciseFileE3 = File.createTempFile("temp_exercise_3",
                ".txt", outputTempExerciseDir);
    } catch (IOException e) {
        e.printStackTrace();
        Log.w("rscReporter", "Encountered an error when creating empty files!");
    }
}

public void writeTemporaryFiles() {
    try {
        if (counterAnotherExercise == 1) {
            writerTemp = new FileWriter(outputTempExerciseFileE1);
            writerTemp
                    .write(editTextExerciseName.getText().toString() + "\n"
                            + counterNoSets + "\n" + counterRepsPerSet
                            + "\n" + counterMeanRepTime + "\n"
                            + counterMeanRepTimeRefined + "\n"
                            + counterSetInterval);
            writerTemp.close();
        } else if (counterAnotherExercise == 2) {
            writerTemp = new FileWriter(outputTempExerciseFileE2);
            writerTemp
                    .write(editTextExerciseName.getText().toString() + "\n"
                            + counterNoSets + "\n" + counterRepsPerSet
                            + "\n" + counterMeanRepTime + "\n"
                            + counterMeanRepTimeRefined + "\n"
                            + counterSetInterval);
            writerTemp.close();
        } else if (counterAnotherExercise == 3) {
            writerTemp = new FileWriter(outputTempExerciseFileE3);
            writerTemp
                    .write(editTextExerciseName.getText().toString() + "\n"
                            + counterNoSets + "\n" + counterRepsPerSet
                            + "\n" + counterMeanRepTime + "\n"
                            + counterMeanRepTimeRefined + "\n"
                            + counterSetInterval);
            writerTemp.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
任何文本文件应如下所示:

editTextExerciseName
counterNoSets
counterRepsPerSet
counterMeanRepTime
counterMeanRepTimeRefined
counterSetInterval
    // In a switch statement as there are around 15 buttons
    case R.id.button_another_exercise_foreground:
        // Increases page number in fragment
        counterAnotherExercise++;
        // This then checks the page number and changes text
        checkPageNo();
        // Writing to files is called, files were created in onCreateView()
        writeTemporaryFiles();
        // Resets all the counters, giving the imitation it is a completely new fragment
        counterReset();
        // default array exercise is then set to the page number which is then displayed as title
        // For example: Exercise 1, Exercise 2, Exercise 3...
        textViewExerciseTitle.setText(defaultArrayExercise);
        break;
调用这两个方法的地方:

editTextExerciseName
counterNoSets
counterRepsPerSet
counterMeanRepTime
counterMeanRepTimeRefined
counterSetInterval
    // In a switch statement as there are around 15 buttons
    case R.id.button_another_exercise_foreground:
        // Increases page number in fragment
        counterAnotherExercise++;
        // This then checks the page number and changes text
        checkPageNo();
        // Writing to files is called, files were created in onCreateView()
        writeTemporaryFiles();
        // Resets all the counters, giving the imitation it is a completely new fragment
        counterReset();
        // default array exercise is then set to the page number which is then displayed as title
        // For example: Exercise 1, Exercise 2, Exercise 3...
        textViewExerciseTitle.setText(defaultArrayExercise);
        break;

我只知道Java和Android的基础知识,对我来说,这是雄心勃勃的,但是,你必须在某个地方学习!欢迎您提供保存值的其他建议。

您实际上不需要文件,因为您只需要写入和读取少量固定数据。这样使用:

写:

PreferenceManager.getDefaultSharedPreferences(YourActivity.this).edit().putString("editTextExerciseName", "my exercise").commit();
全文如下:|

PreferenceManager.getDefaultSharedPreferences(YourActivity.this).getString("editTextExerciseName");

我会在MainActivity中使用它们并从片段中检索变量吗?就因为我在“YourActivity”中出错了。同时,我正在研究共享偏好,谢谢!