Android - 从文本文件中检索特定行或标签并显示在列表视图中?
Android - Retrive particular line or lable from text file and display on a list View?
我有一个文本文件,我能够阅读全部内容并将其显示在视图中。
示例文件:http://pastebin.com/yCmpJfG0
但我的要求是我需要从字段名称中获取特定数据。
例如,我需要从 "Label_1"
中检索 Name_1
、address_1
和 Phone_1
,然后显示在单独的 list view
上。然后 label_2
值等等。
我认为你应该使用JSON:(但我是谁?)
[
{
"Name": "name_1",
"Adress": "adress_1",
"Phone": "phone_1"
},
{
"Name": "name_2",
"Adress": "adress_2",
"Phone": "phone_2"
},
{
"Name": "name_3",
"Adress": "adress_3",
"Phone": "phone_3"
}
]
现在您可以使用JSON数组和JSON对象来检索项目!
ArrayList<Person> personList = new ArrayList<>();
try {
JSONArray jArray = new JSONArray(json);
for (int i = 0; i < jArray.length(); i++) {
try {
JSONObject oneObject = jArray.getJSONObject(i);
String name = oneObject.getString("Name");
String adress = oneObject.getString("Adress");
String phone = oneObject.getString("Phone");
personList.add(new Person(name, adress, phone));
} catch (JSONException e) {
// Oops
}
}
} catch (JSONException e) {
e.printStackTrace();
}
并且您可以将列表中的人员用于 listView。
有多种方法可以解决此问题,这不是 android 问题。以下代码可能会有用(将文本文件放入 res\raw 文件夹,示例 res\raw\test.txt):
(你可以在这个 LINK 中尝试类似的代码)
final String PHONE = "PHONE";
final String ADDRESS = "ADDRESS";
final String NAME = "NAME";
InputStream in_s = getResources().openRawResource(R.raw.test);
byte[] b = "".getBytes();
try {
b = new byte[in_s.available()];
in_s.read(b);
} catch (IOException ex) {
System.out.println("Error!");
}
String[] words = (new String(b)).replace(":: label_", "").split("\n");
HashMap<Integer, HashMap<String, String>> h1 = new HashMap<Integer, HashMap<String, String>>();
int groupNumber = 1;
for (int i = 0; i < words.length; i++) {
String str = words[i];
if (!str.isEmpty()) {
if (str.equals(String.valueOf(groupNumber))) {
HashMap<String, String> h2 = new HashMap<String, String>();
h1.put(groupNumber, h2);
} else {
h1.get(groupNumber).put(PHONE, words[i]);
h1.get(groupNumber).put(ADDRESS, words[++i]);
h1.get(groupNumber).put(NAME, words[++i]);
groupNumber++;
}
}
}
System.out.println(h1);
这段代码显示了一个哈希图:
{1={NAME=<PHONE_1>, ADDRESS=[ADDRESS_1], PHONE=[NAME_1]}, 3={NAME=<PHONE_3>, ADDRESS=[ADDRESS_3], PHONE=[NAME_3]}, 2={NAME=<PHONE_2>, ADDRESS=[ADDRESS_2], PHONE=[NAME_2]}}
您可以通过以下方式获取值:
h1.get(1).get(PHONE);//this returns de phone value from group 1
此代码仅适用于您的文本文件(但您可以针对其他结构进行更改):
:: label_1
[NAME_1]
[ADDRESS_1]
<PHONE_1>
:: label_2
[NAME_2]
[ADDRESS_2]
<PHONE_2>
:: label_3
[NAME_3]
[ADDRESS_3]
<PHONE_3>
(已编辑)
假设文字是http://pastebin.com/fPDm72fe,我改了一段代码:
String[] words = (new String(b)).split("\n");
HashMap<Integer, HashMap<String, String>> h1 = new HashMap<Integer, HashMap<String, String>>();
int groupNumber = 1;
for (int i = 0; i < words.length; i++) {
String str = words[i];
if (!str.trim().isEmpty()) {
if(str.contains("::")){
HashMap<String, String> h2 = new HashMap<String, String>();
h1.put(groupNumber, h2);
} else {
h1.get(groupNumber).put(PHONE, words[i]);
h1.get(groupNumber).put(ADDRESS, words[++i]);
h1.get(groupNumber).put(NAME, words[++i]);
groupNumber++;
}
}
}
你可以试试他的here
我有一个文本文件,我能够阅读全部内容并将其显示在视图中。
示例文件:http://pastebin.com/yCmpJfG0
但我的要求是我需要从字段名称中获取特定数据。
例如,我需要从 "Label_1"
中检索 Name_1
、address_1
和 Phone_1
,然后显示在单独的 list view
上。然后 label_2
值等等。
我认为你应该使用JSON:(但我是谁?)
[
{
"Name": "name_1",
"Adress": "adress_1",
"Phone": "phone_1"
},
{
"Name": "name_2",
"Adress": "adress_2",
"Phone": "phone_2"
},
{
"Name": "name_3",
"Adress": "adress_3",
"Phone": "phone_3"
}
]
现在您可以使用JSON数组和JSON对象来检索项目!
ArrayList<Person> personList = new ArrayList<>();
try {
JSONArray jArray = new JSONArray(json);
for (int i = 0; i < jArray.length(); i++) {
try {
JSONObject oneObject = jArray.getJSONObject(i);
String name = oneObject.getString("Name");
String adress = oneObject.getString("Adress");
String phone = oneObject.getString("Phone");
personList.add(new Person(name, adress, phone));
} catch (JSONException e) {
// Oops
}
}
} catch (JSONException e) {
e.printStackTrace();
}
并且您可以将列表中的人员用于 listView。
有多种方法可以解决此问题,这不是 android 问题。以下代码可能会有用(将文本文件放入 res\raw 文件夹,示例 res\raw\test.txt):
(你可以在这个 LINK 中尝试类似的代码)
final String PHONE = "PHONE";
final String ADDRESS = "ADDRESS";
final String NAME = "NAME";
InputStream in_s = getResources().openRawResource(R.raw.test);
byte[] b = "".getBytes();
try {
b = new byte[in_s.available()];
in_s.read(b);
} catch (IOException ex) {
System.out.println("Error!");
}
String[] words = (new String(b)).replace(":: label_", "").split("\n");
HashMap<Integer, HashMap<String, String>> h1 = new HashMap<Integer, HashMap<String, String>>();
int groupNumber = 1;
for (int i = 0; i < words.length; i++) {
String str = words[i];
if (!str.isEmpty()) {
if (str.equals(String.valueOf(groupNumber))) {
HashMap<String, String> h2 = new HashMap<String, String>();
h1.put(groupNumber, h2);
} else {
h1.get(groupNumber).put(PHONE, words[i]);
h1.get(groupNumber).put(ADDRESS, words[++i]);
h1.get(groupNumber).put(NAME, words[++i]);
groupNumber++;
}
}
}
System.out.println(h1);
这段代码显示了一个哈希图:
{1={NAME=<PHONE_1>, ADDRESS=[ADDRESS_1], PHONE=[NAME_1]}, 3={NAME=<PHONE_3>, ADDRESS=[ADDRESS_3], PHONE=[NAME_3]}, 2={NAME=<PHONE_2>, ADDRESS=[ADDRESS_2], PHONE=[NAME_2]}}
您可以通过以下方式获取值:
h1.get(1).get(PHONE);//this returns de phone value from group 1
此代码仅适用于您的文本文件(但您可以针对其他结构进行更改):
:: label_1
[NAME_1]
[ADDRESS_1]
<PHONE_1>
:: label_2
[NAME_2]
[ADDRESS_2]
<PHONE_2>
:: label_3
[NAME_3]
[ADDRESS_3]
<PHONE_3>
(已编辑) 假设文字是http://pastebin.com/fPDm72fe,我改了一段代码:
String[] words = (new String(b)).split("\n");
HashMap<Integer, HashMap<String, String>> h1 = new HashMap<Integer, HashMap<String, String>>();
int groupNumber = 1;
for (int i = 0; i < words.length; i++) {
String str = words[i];
if (!str.trim().isEmpty()) {
if(str.contains("::")){
HashMap<String, String> h2 = new HashMap<String, String>();
h1.put(groupNumber, h2);
} else {
h1.get(groupNumber).put(PHONE, words[i]);
h1.get(groupNumber).put(ADDRESS, words[++i]);
h1.get(groupNumber).put(NAME, words[++i]);
groupNumber++;
}
}
}
你可以试试他的here