【问题标题】:Managing data in android cache?管理android缓存中的数据?
【发布时间】:2014-07-21 06:57:45
【问题描述】:

我试图在接下来的 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 的基础知识,对我自己来说这是雄心勃勃的,但是,你必须在某个地方学习!欢迎提供有关保存值的其他建议。

【问题讨论】:

    标签: android caching


    【解决方案1】:

    您实际上并不需要文件,因为您只是在写入然后读取少量固定数据。像这样使用SharedPreferences

    写:

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

    阅读:|

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

    【讨论】:

    • 我会在 MainActivity 中使用这些并从片段中检索变量吗?只是因为我在“YourActivity”中遇到错误。同时,我正在研究共享偏好,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    相关资源
    最近更新 更多