我是 Android 开发新手,我已经尝试过一些。我试图创建一个程序,该程序具有一个类似数据库的小型不变数据集合。在我目前最好的语言 C# 中,我将使用自定义类的列表并将其序列化为 xml 文件,然后在运行时将其读入我的应用程序。我在 Android 中找到了 /xml 资源文件夹,但我不确定如何去做我设想的事情。这样做的最佳方法是什么?
数据永远不需要改变。示例:
Blob | A | B
----------------
Blob 1 | 23 | 42
Blob 2 | 34 | 21
我知道它的布局就像表格一样,但是使用数据库对我来说并没有真正的意义,因为数据永远不会改变,而且我需要一种方法来存储它以最初填充数据库。
所以基本上我正在寻找一种在我的应用程序中存储有些复杂的静态数据的方法。有什么想法吗?
编辑:我还看到了 /raw 文件夹。所以我可以将东西存储在 /res/raw 或 /res/xml 中。但我不确定存储/解析数据的最佳方式是什么......
I'm new to Android development, and I've been playing around with it a bit. I was trying to create a program that has a small database-like collection of never-changing data. In C#, my currently best language, I'd use a List of a custom class and serialize that to an xml file, then read that into my application at runtime. I found the /xml resource folder in Android, but I'm not sure how I would go about doing what I'm envisioning. What would be the best way to go about doing this?
The data will never need to change. Example:
Blob | A | B
----------------
Blob 1 | 23 | 42
Blob 2 | 34 | 21
I know that's laid out like a table, but using a database doesn't really make sense to me because the data will never change, and I would need a way to store it to initially populate the database anyway.
So basically I'm looking for a way to store somewhat-complex static data in my application. Any ideas?
EDIT: I also saw the /raw folder. So I could store things in /res/raw or /res/xml. But I'm not sure what would be the best way to store/parse the data...
发布评论
评论(4)
我认为这是最好的解决方案,我已经使用这个解决方案在我的每个项目中存储静态数据。
为此...
您可以做一件事,制作一个 xml 文件,即“temp.xml”..并将数据存储在 temp.xml 中,如下所示:
然后使用 XML PullParser 技术来解析数据。
您可以在示例上获得 PullParsing 技术的编码示例,请参考此更好的想法的例子。
享受!!
I think this is the BEST solution and i am already using this one to store Static-data in my every project.
For that...
You can do one thing, make one xml file namely "temp.xml" ..and store the data in temp.xml as follows:
and then use XML PullParser technique to parse data.
You can have coding samples of PullParsing technique on Example , refer this example for better idea.
Enjoy!!
最好的方法是使用 Android 资源层次结构。
在 res/values/ 目录中,您可以存储多种基本数据类型的任意数量的键值对。在您的应用程序中,您将使用自动生成的资源 ID(基于您的资源密钥的名称)来引用它们。请参阅上面的链接以获取更多文档和详细信息。
Android 还支持原始数据文件。您可以将数据存储在 res/raw/yourfile.dat 下的文件目录中。
您可以以所需的任何基于文本的格式创建数据,然后在活动启动时使用资源访问 api 读取它。
The best way is to use the Android Resource Heirarchy.
In the res/values/ directory, you can store any number of key-value pairs for several basic data types. In your app, you would refer to them using an autogenerated resource id (name based on your resource's key). See the link above for more documentation and details.
Android also supports raw datafiles. You could store your data in the file directory under res/raw/yourfile.dat
You you create your data in whatever text based format you want and then read it on activity startup using the resource access apis.
我过去曾使用 Simple 进行 xml 解析。我认为如果您知道 xml 中的内容(在您的情况下您知道),那么它的代码量最少。
http://simple.sourceforge.net/
I have used Simple for xml parsing in the past. I think it has the least amount of code if you know what to expect in xml, which in your case you do.
http://simple.sourceforge.net/
根据 doc,
/xml
是可行的方法。提供资源
xml/
任意运行时可以通过调用读取的XML文件各种XML配置文件必须保存在这里,例如可搜索的配置。
getXML() 的文档
我还做了一个工作示例:
XML 结构:
保存解析数据的 Java 类:
解析器本身:
<前><代码>/**
* 由 bivanbi 创建于 2017.02.23..
*
* 将包含测验数据的 xml 资源解析为 QuizQuestion 对象的 ArrayList 的类
*
*/
公共类 QuizXmlParser {
公共静态字符串lastErrorMessage =“”;
/**;解析(活动活动,int xmlResourceId)
* 将 XML 数据解析为 QuizQuestion 对象的 ArrayList 的静态方法
* @param Activity 是调用 Activity
* @param xmlResourceId 为待解析的XML资源的资源id
* 如果发生解析错误则返回 null,如果成功则返回对象的 ArrayList
* @抛出XmlPullParserException
* @抛出IOException
*/
public static ArrayList
抛出 XmlPullParserException、IOException
{
String logTag = QuizXmlParser.class.getSimpleName();
资源 res = Activity.getResources();
XmlResourceParser quizDataXmlParser = res.getXml(R.xml.quiz_data);
ArrayList<字符串> xmlTagStack = new ArrayList<>();
ArrayList<测验问题> quizQuestions = new ArrayList<>();
测验问题当前问题 = null;
布尔值 isCurrentAnswerCorrect = false;
quizDataXmlParser.next();
int eventType = quizDataXmlParser.getEventType();
while (eventType!= XmlPullParser.END_DOCUMENT)
{
// 开始文档
if(eventType == XmlPullParser.START_DOCUMENT)
{
Log.d(logTag,"开始文档");
}
// 开始标签
否则 if(eventType == XmlPullParser.START_TAG)
{
字符串 tagName = quizDataXmlParser.getName();
xmlTagStack.add(tagName);
Log.d(logTag,"开始标签"+tagName+",深度:"+xmlTagStack.size());
Log.d(logTag,"标签 "+tagName+" 具有 "+quizDataXmlParser.getAttributeCount()+" 属性");
// 这是测验问题标签的开始,因此创建一个新的 QuizQuestion 对象
if (tagName.equals("测验问题")){
当前问题 = new QuizQuestion();
}
else if(tagName.equals("answer"))
{
isCurrentAnswerCorrect = quizDataXmlParser.getAttributeBooleanValue(null,"正确",false);
if (isCurrentAnswerCorrect == true) {
Log.d(logTag, "标签 " + tagName + " 具有正确属性 = true");
}
别的
{
Log.d(logTag, "标签 " + tagName + " 具有正确属性 = false");
}
}
}
// 结束标记
否则 if(eventType == XmlPullParser.END_TAG)
{
字符串 tagName = quizDataXmlParser.getName();
如果 (xmlTagStack.size() < 1)
{
lastErrorMessage = "错误 101: 当 TagStack 为空时遇到 END_TAG "+quizDataXmlParser.getName()+";
Log.e(logTag, 最后的错误消息);
返回空值;
}
xmlTagStack.remove(xmlTagStack.size()-1);
Log.d(logTag,"结束标签"+quizDataXmlParser.getName()+", 深度:"+xmlTagStack.size());
// 到达测验问题定义的末尾,将其添加到数组中
if (tagName.equals("测验问题")){
if (当前问题!= null)
quizQuestions.add(currentQuestion);
当前问题 = null;
}
}
// 标签开始和结束之间的文本
else if(eventType == XmlPullParser.TEXT)
{
字符串 currentTag = xmlTagStack.get(xmlTagStack.size()-1);
字符串文本 = quizDataXmlParser.getText();
Log.d(logTag,"文本:"+text+",当前标签:"+currentTag+",深度:"+xmlTagStack.size());
if (当前问题 == null) {
Log.e(logTag,"currentQuestion未初始化!text:"+text+",当前标签:"+currentTag+",深度:"+xmlTagStack.size());
继续;
}
if (currentTag.equals("header_image_src"))
{
int drawableResourceId = Activity.getResources().getIdentifier(text, "drawable", Activity.getPackageName());
currentQuestion.setHeaderImageResId(drawableResourceId);
}
else if (currentTag.equals("问题"))
{
currentQuestion.setQuestion(文本);
}
else if (currentTag.equals("answer"))
{
currentQuestion.addAnswer(text, isCurrentAnswerCorrect);
}
else if (currentTag.equals("input_type"))
{
currentQuestion.setInputType(文本);
}
别的
{
Log.e(logTag,"意外标签"+currentTag+",文本:"+text+",深度:"+xmlTagStack.size());
}
}
eventType = quizDataXmlParser.next();
}
Log.d(logTag,"结束文档");
返回测验问题;
}
}
最后,调用解析器:
According to the doc,
/xml
is the way to go.Providing Resources
xml/
Arbitrary XML files that can be read at run-time by callingVarious XML configuration files must be saved here, such as a searchable configuration.
Documentation for
getXML()
I also made a working example:
the XML structure:
the Java class to hold parsed data:
the parser itself:
and finally, calling the parser: