-
Notifications
You must be signed in to change notification settings - Fork 44
/
Divisions.java
79 lines (67 loc) · 2.43 KB
/
Divisions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package top.defaults.pickerviewapp;
import android.content.Context;
import android.util.SparseArray;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Divisions {
private static List<DivisionModel> divisions;
private static DivisionModel parse(JSONObject o) {
DivisionModel division = new DivisionModel();
try {
division.id = o.getInt("id");
division.name = o.getString("name");
division.lvl = o.getInt("lvl");
division.parentId = o.getInt("parent");
} catch (JSONException e) {
e.printStackTrace();
}
return division;
}
public static List<DivisionModel> get(Context context) {
if (divisions != null) {
return divisions;
}
divisions = new ArrayList<>(4000);
SparseArray<DivisionModel> divisionMap = new SparseArray<>(4000);
try {
JSONArray array = new JSONArray(readJson(context));
for (int i = 0; i < array.length(); i++) {
DivisionModel division = Divisions.parse(array.getJSONObject(i));
if (division.lvl == 1) divisions.add(division);
divisionMap.put(division.id, division);
}
for (int i = 0; i < divisionMap.size(); i++) {
DivisionModel division = divisionMap.valueAt(i);
if (division.parentId != 0) {
division.parent = divisionMap.get(division.parentId);
if (division.parent.children == null) {
division.parent.children = new ArrayList<>(40);
}
division.parent.children.add(division);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return divisions;
}
private static String readJson(Context context) {
InputStream inputStream;
try {
inputStream = context.getAssets().open("area.json");
int size = inputStream.available();
byte[] bytes = new byte[size];
size = inputStream.read(bytes);
inputStream.close();
return new String(bytes, 0, size);
} catch (IOException e) {
e.printStackTrace();
}
return "[]";
}
}