延迟读取 Android 中的 .txt 文件
Reading .txt files in Android with a delay
我是 Android Studio 的初学者,我当前的项目从 .txt 文件中读取单词(基本上是字典)并向用户输出一个随机单词。用户又输入另一个单词,其中一个字符与显示给他的字符不同。一切正常,除了当我开始阅读另一个 .txt 文件(在本例中为另一种语言的字典)时,程序会放慢速度以从该文件中读取所有这些单词。我基本上只读一次所有单词,然后将它们添加到字符串数组列表中,以便稍后处理。在这一点上,我不知道瞬间延迟问题的根源可能是什么,因为每个 .txt 文件中最多有 1-2 千个单词,我认为手机足够快,可以一次阅读它们,但我是反正对此一无所知。我知道 .txt 有更好的替代方法,比如使用 sql,但我现在熟悉阅读 .txt 文件,现在想继续使用它。有人可以推荐我一些东西来解决那一时的滞后吗?先感谢您。这是我的代码,当语言改变时调用这两个方法:
public void restartGame() throws IOException {
//availableWords is the list of all words
availableWords = new ArrayList<>();
//currentStream is an InputStream targeted to the current .txt file
currentStream.reset();
//I read all the words and add them to the arraylist
Scanner reader = new Scanner(currentStream);
while (reader.hasNext())
availableWords.add(reader.next());
//I choose a random word from the arraylist to begin with
String chosenWord = availableWords.get((int) (availableWords.size() * Math.random()));
wordOutput.setText(chosenWord);
availableWords.remove(chosenWord);
String previousText = "";
for( int i = 0; i < 4; i++) {
for (int chars = 0; chars < currentWordLength; chars++)
previousText += " ";
previousText += "\n";
}
previousWords.setText(previousText);
wordInput.setText("");
restartButton.setVisibility(View.GONE);
wordInput.setVisibility(View.VISIBLE);
enterButton.setVisibility(View.VISIBLE);
}
//a spinner to let user select a language
languageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//chosen language is assigned to a variable
currentLanguage = parent.getSelectedItem().toString();
//corresponding inputStream is assigned to currentStream so that in restartGame method, the game restarts with the chosen language
ArrayList<InputStream> wordLengthLanguage = wordsFile.get(currentWordLength - 3);
currentStream = wordLengthLanguage.get(languages.indexOf(currentLanguage));
try {
restartGame();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
从文件中读取单词的目的或目标是什么?如果您想保留数据(如 sqlite),您可以创建一个包含字符串数组的字符串资源。每次应用程序启动时,您都可以从资源中读取它们。
首先,转到 android studio 上的应用程序文件夹,然后 res > values
右键单击值,然后 new > values resource file
。随心所欲的名字。在资源标签中创建如下内容:
<string-array name="words">
<item>Word 1</item>
<item>Word 2</item>
</string-array>
当您需要数组时,只需执行以下操作:
getResources().getStringArray(R.array.words)
其中 "words" 是字符串数组的名称。请注意,如果您在片段上,则需要 Context 才能访问资源。
这个数组是不可编辑的,所以你不能在执行时添加更多的词,所有的词都需要在资源文件中之前创建。
希望对你有帮助。
我是 Android Studio 的初学者,我当前的项目从 .txt 文件中读取单词(基本上是字典)并向用户输出一个随机单词。用户又输入另一个单词,其中一个字符与显示给他的字符不同。一切正常,除了当我开始阅读另一个 .txt 文件(在本例中为另一种语言的字典)时,程序会放慢速度以从该文件中读取所有这些单词。我基本上只读一次所有单词,然后将它们添加到字符串数组列表中,以便稍后处理。在这一点上,我不知道瞬间延迟问题的根源可能是什么,因为每个 .txt 文件中最多有 1-2 千个单词,我认为手机足够快,可以一次阅读它们,但我是反正对此一无所知。我知道 .txt 有更好的替代方法,比如使用 sql,但我现在熟悉阅读 .txt 文件,现在想继续使用它。有人可以推荐我一些东西来解决那一时的滞后吗?先感谢您。这是我的代码,当语言改变时调用这两个方法:
public void restartGame() throws IOException {
//availableWords is the list of all words
availableWords = new ArrayList<>();
//currentStream is an InputStream targeted to the current .txt file
currentStream.reset();
//I read all the words and add them to the arraylist
Scanner reader = new Scanner(currentStream);
while (reader.hasNext())
availableWords.add(reader.next());
//I choose a random word from the arraylist to begin with
String chosenWord = availableWords.get((int) (availableWords.size() * Math.random()));
wordOutput.setText(chosenWord);
availableWords.remove(chosenWord);
String previousText = "";
for( int i = 0; i < 4; i++) {
for (int chars = 0; chars < currentWordLength; chars++)
previousText += " ";
previousText += "\n";
}
previousWords.setText(previousText);
wordInput.setText("");
restartButton.setVisibility(View.GONE);
wordInput.setVisibility(View.VISIBLE);
enterButton.setVisibility(View.VISIBLE);
}
//a spinner to let user select a language
languageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//chosen language is assigned to a variable
currentLanguage = parent.getSelectedItem().toString();
//corresponding inputStream is assigned to currentStream so that in restartGame method, the game restarts with the chosen language
ArrayList<InputStream> wordLengthLanguage = wordsFile.get(currentWordLength - 3);
currentStream = wordLengthLanguage.get(languages.indexOf(currentLanguage));
try {
restartGame();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
从文件中读取单词的目的或目标是什么?如果您想保留数据(如 sqlite),您可以创建一个包含字符串数组的字符串资源。每次应用程序启动时,您都可以从资源中读取它们。
首先,转到 android studio 上的应用程序文件夹,然后 res > values
右键单击值,然后 new > values resource file
。随心所欲的名字。在资源标签中创建如下内容:
<string-array name="words">
<item>Word 1</item>
<item>Word 2</item>
</string-array>
当您需要数组时,只需执行以下操作:
getResources().getStringArray(R.array.words)
其中 "words" 是字符串数组的名称。请注意,如果您在片段上,则需要 Context 才能访问资源。
这个数组是不可编辑的,所以你不能在执行时添加更多的词,所有的词都需要在资源文件中之前创建。
希望对你有帮助。