Android 什么';存储文本数据的最佳方式是什么?
我对Android开发有点陌生,但我编写了很多C#(WinF,WPF)。我为应用程序创建了一个测验应用程序(德语单词),但我不太确定如何存储和加载字典(一个文件,其中行包含2个单词)。存储这些词典的最佳方式是什么?我在谷歌上搜索了一下,但没有找到确切的答案。现在我直接在代码中生成单词。Android 什么';存储文本数据的最佳方式是什么?,android,android-file,Android,Android File,我对Android开发有点陌生,但我编写了很多C#(WinF,WPF)。我为应用程序创建了一个测验应用程序(德语单词),但我不太确定如何存储和加载字典(一个文件,其中行包含2个单词)。存储这些词典的最佳方式是什么?我在谷歌上搜索了一下,但没有找到确切的答案。现在我直接在代码中生成单词。 谢谢 由于您只有一个键值对,我建议您从数据中创建一个json,存储到Assets文件夹中,并在运行时使用 例如,CountyCode.json [ { "country_name": "Canad
谢谢 由于您只有一个键值对,我建议您从数据中创建一个json,存储到Assets文件夹中,并在运行时使用 例如,CountyCode.json
[
{
"country_name": "Canada",
"country_code": 1
},
{
"country_name": "United States of America",
"country_code": 1
},
{
"country_name": "US Virgin Islands",
"country_code": 1
},
{
"country_name": "Russia",
"country_code": 7
},
{
"country_name": "Tajikistan",
"country_code": 7
}]
try {
JSONArray jsonArray = new JSONArray(countryJson);
if (jsonArray != null) {
final String[] items = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
items[i] = jsonObject.getString("country_name");
}
在需要时使用以下代码加载和解析json数据
从Assets文件夹加载json
[
{
"country_name": "Canada",
"country_code": 1
},
{
"country_name": "United States of America",
"country_code": 1
},
{
"country_name": "US Virgin Islands",
"country_code": 1
},
{
"country_name": "Russia",
"country_code": 7
},
{
"country_name": "Tajikistan",
"country_code": 7
}]
try {
JSONArray jsonArray = new JSONArray(countryJson);
if (jsonArray != null) {
final String[] items = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
items[i] = jsonObject.getString("country_name");
}
String countryJson=FileManager.getFileManager().loadJSONFromAsset(getActivity(),“countrycode.json”)
解析json并使用
[
{
"country_name": "Canada",
"country_code": 1
},
{
"country_name": "United States of America",
"country_code": 1
},
{
"country_name": "US Virgin Islands",
"country_code": 1
},
{
"country_name": "Russia",
"country_code": 7
},
{
"country_name": "Tajikistan",
"country_code": 7
}]
try {
JSONArray jsonArray = new JSONArray(countryJson);
if (jsonArray != null) {
final String[] items = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
items[i] = jsonObject.getString("country_name");
}
您可以将其存储在Assets文件夹中,并可以访问文件。FileManager类从何而来?为FileManager类添加了代码。非常感谢您,很抱歉现在接受了答案。我还没试过,今晚我要试一下。