Android,存储和检索文本
Android, Storing and retrieving Text
我目前正在为我的大学准备 Android 申请,让我给你做一个 swift 演示:
有一个故事可以让玩家阅读,然后他有3个选择,他会选择一个,然后另一个故事会再次出现,有3个选择,如此继续......(但是每一个选择导致一个完全不同的故事)
如您所料,我有很多文本要存储、class 和检索。第一页显示第一个故事和 3 个选择,如果玩家选择并单击一个按钮,将出现以下故事,但它也可能意味着结束(显然,如果我始终保持 3 条路径 X 3 条路径 X 3 条路径......它会变得复杂)
_______________ 我在想什么 _____________________________
所以我的第一个猜测是使用 xml 文件,一个包含每个故事,另一个包含每个选择。
在应用程序开始时,我会使用解析器 运行 遍历文件并将每个字符串放入树结构中。 (所以我想一棵树代表故事,另一棵树代表选择)
我想用数字来构造它,比如你现在在1,你点选2然后你在上一个故事的末尾再放一个2,它就会变成12,然后如果你点击选项 3,它变成 123 等等...
我虽然这对树来说更容易,唉,xml 只给了一个标签名称,所以我想使用 story1、story11、story12、story13 等...
What the structure of my xml should look like (or my tree)
但是在树中 class 它已经很麻烦了,因为我想使用数字,我必须用 parser.parName() 得到一个字符串,然后我需要子字符串来得到最后的数字,只是为了检索它应该去哪个节点需要一些资源。
换句话说,我想得越多,想法就越复杂,所以我需要一些聪明的人告诉我该去哪里。 (明明已经在路上迷路了)
__________________________________ 我的问题 ____________________________
如果你觉得自己很聪明,这是我的问题
你会用什么来存储文本? Xml 文件?之后你会用一棵树吗?您将如何做到这一点,以有效地 class 将 xml 放入树中?
看来我的解释有点乱,如果有不明白的地方请告诉我。 (或者如果是我蹩脚的英语)
感谢您的回答!
就个人而言,如果不需要翻译您的任何资产,我会将每个场景放在一个 JSON 文件中,并且每个选择都有一个指向下一个文件名的键。您可以在运行时加载第一个文件(或最后保存的文件)。场景的文件名将是 "save" 本身。
但是,如果您需要翻译任何故事情节,您可能希望将其转储到 "strings.xml" 中,然后在运行时创建树并引用各自的 R.string id。 R.string id 不应在这种情况下用作保存,因为它可以在编译之间更改。每个故事场景都应该有一个永远不应修改的场景 ID(枚举即可)
稍微思考一下这个问题后,我基本上想到了与 Kenny Byun 的回答类似的想法,但我想用一些实际代码来充实它,向您展示它是如何工作的。
首先,我会将每个单独的故事存储在它自己的 JSON 对象中。我不知道它们是否需要像 Kenny 建议的那样位于单独的文件中,因为它们可以很容易地包含在单个文件中 JSON 数组中的单独对象中。结构如下。
数组中的每个对象都是一个 "story" 对象。这包含一个索引或某种 id 以将其标识为一个独特的故事。然后它包含它的故事文本和一个包含 3 "choice" 个对象的数组。选择对象的相似之处在于它们每个都包含一个 id 或索引、一些文本和一个 "leads_to_story" 值,它基本上标识了该选择导致的故事。它看起来像这样...
[
{
"story_index" : 0,
"story_text" : "It was a dark and stormy night. Three doors stand before you. You must choose one.",
"choices" : [
{
"choice_index" : 0,
"choice_text" : "Door 1",
"leads_to_story" : 1
},
{
"choice_index" : 1,
"choice_text" : "Door 2",
"leads_to_story" : 2
},
{
"choice_index" : 2,
"choice_text" : "Door 3",
"leads_to_story" : 3
}
]
},
{
"story_index" : 1,
"story_text" : "You choose door 1 and enter cautiously. You find a bunny rabbit.",
"choices" : [
{
"choice_index" : 0,
"choice_text" : "Pet the bunny rabbit.",
"leads_to_story" : 4
},
{
"choice_index" : 1,
"choice_text" : "Kill the bunny rabbit.",
"leads_to_story" : 5
},
{
"choice_index" : 2,
"choice_text" : "Ask the bunny rabbit its name.",
"leads_to_story" : 6
}
]
}
]
既然您已经以有组织的方式存储了所有故事和选择数据,我将创建两个 classes 来存储这些数据以供在运行时访问。所以我会创建一个故事 class 和一个选择 class。他们可能看起来像这样...
public class Story {
public int index;
public String text;
public Choice[] choices;
public Story(int index, String text, Choice[] choices) {
this.index = index;
this.text = text;
this.choices = choices;
}
}
public class Choice {
public String text;
public int leads_to_story;
public Choice(String text, int leads_to_story) {
this.text = text;
this.leads_to_story = leads_to_story;
}
}
这将允许您将所有故事数据加载到有组织的对象中,然后可以从按数字顺序放置的故事对象数组访问这些对象。因此,当您需要某个 Story 时,只需从 Array 中调用该索引即可。您的工作流程可能看起来像这样...
//if this is a new game...
//Load and parse the JSON into a local array,
//placing the stories in numerical order
ArrayList<Story> stories = getStories();
//Present story 0, since this is a new game...
TextView textView = (TextView) findViewById(R.id.story_text);
Story story = stories.get(0);
String storyText = story.text;
textView.setText(storyText);
//The player makes his choice, so you now
//get the leads_to_story value to find out
//where to go next...
int playerChoice = 1; //whatever they choose
int nextStory = story.choices[playerChoice].leads_to_story;
//New screen loads or is initialized,
//and we load the new story...
Story story = stories.get(nextStory);
//...repeat the process...
对于 "death" 场景,我可能只有一个索引为 -1 或类似内容的故事对象。那么你就可以随时测试 leads_to_story < 0
是否玩家死亡。
希望这能给您一些关于如何使事情易于管理和组织的想法。在这些类型的场景中,我总是很想尝试找到 "clever" 处理数据的方法,但通常情况下,一种简单实用的方法效果更好,并且使事情更具可读性和可管理性。希望这对您有所帮助!
我目前正在为我的大学准备 Android 申请,让我给你做一个 swift 演示:
有一个故事可以让玩家阅读,然后他有3个选择,他会选择一个,然后另一个故事会再次出现,有3个选择,如此继续......(但是每一个选择导致一个完全不同的故事)
如您所料,我有很多文本要存储、class 和检索。第一页显示第一个故事和 3 个选择,如果玩家选择并单击一个按钮,将出现以下故事,但它也可能意味着结束(显然,如果我始终保持 3 条路径 X 3 条路径 X 3 条路径......它会变得复杂)
_______________ 我在想什么 _____________________________
所以我的第一个猜测是使用 xml 文件,一个包含每个故事,另一个包含每个选择。
在应用程序开始时,我会使用解析器 运行 遍历文件并将每个字符串放入树结构中。 (所以我想一棵树代表故事,另一棵树代表选择)
我想用数字来构造它,比如你现在在1,你点选2然后你在上一个故事的末尾再放一个2,它就会变成12,然后如果你点击选项 3,它变成 123 等等... 我虽然这对树来说更容易,唉,xml 只给了一个标签名称,所以我想使用 story1、story11、story12、story13 等...
What the structure of my xml should look like (or my tree)
但是在树中 class 它已经很麻烦了,因为我想使用数字,我必须用 parser.parName() 得到一个字符串,然后我需要子字符串来得到最后的数字,只是为了检索它应该去哪个节点需要一些资源。
换句话说,我想得越多,想法就越复杂,所以我需要一些聪明的人告诉我该去哪里。 (明明已经在路上迷路了)
__________________________________ 我的问题 ____________________________
如果你觉得自己很聪明,这是我的问题
你会用什么来存储文本? Xml 文件?之后你会用一棵树吗?您将如何做到这一点,以有效地 class 将 xml 放入树中?
看来我的解释有点乱,如果有不明白的地方请告诉我。 (或者如果是我蹩脚的英语)
感谢您的回答!
就个人而言,如果不需要翻译您的任何资产,我会将每个场景放在一个 JSON 文件中,并且每个选择都有一个指向下一个文件名的键。您可以在运行时加载第一个文件(或最后保存的文件)。场景的文件名将是 "save" 本身。
但是,如果您需要翻译任何故事情节,您可能希望将其转储到 "strings.xml" 中,然后在运行时创建树并引用各自的 R.string id。 R.string id 不应在这种情况下用作保存,因为它可以在编译之间更改。每个故事场景都应该有一个永远不应修改的场景 ID(枚举即可)
稍微思考一下这个问题后,我基本上想到了与 Kenny Byun 的回答类似的想法,但我想用一些实际代码来充实它,向您展示它是如何工作的。
首先,我会将每个单独的故事存储在它自己的 JSON 对象中。我不知道它们是否需要像 Kenny 建议的那样位于单独的文件中,因为它们可以很容易地包含在单个文件中 JSON 数组中的单独对象中。结构如下。
数组中的每个对象都是一个 "story" 对象。这包含一个索引或某种 id 以将其标识为一个独特的故事。然后它包含它的故事文本和一个包含 3 "choice" 个对象的数组。选择对象的相似之处在于它们每个都包含一个 id 或索引、一些文本和一个 "leads_to_story" 值,它基本上标识了该选择导致的故事。它看起来像这样...
[
{
"story_index" : 0,
"story_text" : "It was a dark and stormy night. Three doors stand before you. You must choose one.",
"choices" : [
{
"choice_index" : 0,
"choice_text" : "Door 1",
"leads_to_story" : 1
},
{
"choice_index" : 1,
"choice_text" : "Door 2",
"leads_to_story" : 2
},
{
"choice_index" : 2,
"choice_text" : "Door 3",
"leads_to_story" : 3
}
]
},
{
"story_index" : 1,
"story_text" : "You choose door 1 and enter cautiously. You find a bunny rabbit.",
"choices" : [
{
"choice_index" : 0,
"choice_text" : "Pet the bunny rabbit.",
"leads_to_story" : 4
},
{
"choice_index" : 1,
"choice_text" : "Kill the bunny rabbit.",
"leads_to_story" : 5
},
{
"choice_index" : 2,
"choice_text" : "Ask the bunny rabbit its name.",
"leads_to_story" : 6
}
]
}
]
既然您已经以有组织的方式存储了所有故事和选择数据,我将创建两个 classes 来存储这些数据以供在运行时访问。所以我会创建一个故事 class 和一个选择 class。他们可能看起来像这样...
public class Story {
public int index;
public String text;
public Choice[] choices;
public Story(int index, String text, Choice[] choices) {
this.index = index;
this.text = text;
this.choices = choices;
}
}
public class Choice {
public String text;
public int leads_to_story;
public Choice(String text, int leads_to_story) {
this.text = text;
this.leads_to_story = leads_to_story;
}
}
这将允许您将所有故事数据加载到有组织的对象中,然后可以从按数字顺序放置的故事对象数组访问这些对象。因此,当您需要某个 Story 时,只需从 Array 中调用该索引即可。您的工作流程可能看起来像这样...
//if this is a new game...
//Load and parse the JSON into a local array,
//placing the stories in numerical order
ArrayList<Story> stories = getStories();
//Present story 0, since this is a new game...
TextView textView = (TextView) findViewById(R.id.story_text);
Story story = stories.get(0);
String storyText = story.text;
textView.setText(storyText);
//The player makes his choice, so you now
//get the leads_to_story value to find out
//where to go next...
int playerChoice = 1; //whatever they choose
int nextStory = story.choices[playerChoice].leads_to_story;
//New screen loads or is initialized,
//and we load the new story...
Story story = stories.get(nextStory);
//...repeat the process...
对于 "death" 场景,我可能只有一个索引为 -1 或类似内容的故事对象。那么你就可以随时测试 leads_to_story < 0
是否玩家死亡。
希望这能给您一些关于如何使事情易于管理和组织的想法。在这些类型的场景中,我总是很想尝试找到 "clever" 处理数据的方法,但通常情况下,一种简单实用的方法效果更好,并且使事情更具可读性和可管理性。希望这对您有所帮助!