管理android缓存中的数据?

时间:2014-07-20 22:38:37

标签: android caching

我正试图在接下来的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;

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

1 个答案:

答案 0 :(得分:1)

你不需要文件,因为你只是在写,然后阅读一些固定数据。像这样使用SharedPreferences

写:

PreferenceManager.getDefaultSharedPreferences(YourActivity.this).edit().putString("editTextExerciseName", "my exercise").commit();

阅读:|

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