Idée: placez le code HTML dans des fichiers au format JSON et stockez-les dans / res / raw. (JSON est moins difficile)
Stockez les enregistrements de données comme celui-ci dans un objet de tableau:
[
{
"Field1": "String data",
"Field2": 12345,
"Field3": "more Strings",
"Field4": true
},
{
"Field1": "String data",
"Field2": 12345,
"Field3": "more Strings",
"Field4": true
},
{
"Field1": "String data",
"Field2": 12345,
"Field3": "more Strings",
"Field4": true
}
]
Pour lire les données dans votre application:
private ArrayList<Data> getData(String filename) {
ArrayList<Data> dataArray = new ArrayList<Data>();
try {
int id = getResources().getIdentifier(filename, "raw", getPackageName());
InputStream input = getResources().openRawResource(id);
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
String text = new String(buffer);
Gson gson = new Gson();
Type dataType = new TypeToken<List<Map<String, Object>>>() {}.getType();
List<Map<String, Object>> natural = gson.fromJson(text, dataType);
// now cycle through each object and gather the data from each field
for(Map<String, Object> json : natural) {
final Data ad = new Data(json.get("Field1"), json.get("Field2"), json.get("Field3"), json.get("Field4"));
dataArray.add(ad);
}
} catch (Exception e) {
e.printStackTrace();
}
return dataArray;
}
Enfin, la classe Data
n'est qu'un conteneur de variables publiques pour un accès facile ...
public class Data {
public String string;
public Integer number;
public String somestring;
public Integer site;
public boolean logical;
public Data(String string, Integer number, String somestring, boolean logical)
{
this.string = string;
this.number = number;
this.somestring = somestring;
this.logical = logical;
}
}