Lesen Sie 2d Array-Werte aus der Datei Android - Java, Android, Datei, Shared Preferences, Multidimensional-Array

Hi Ich möchte, dass mein D2 int-Array seine Werte abrufteine Datei. Wenn die Datei nicht existiert, möchte ich sie erstellen und alle Werte des Array auf 0 setzen. Wenn sich die Werte im Array ändern, möchte ich auf meine Schaltfläche zum Speichern klicken, damit sich die Werte in der Datei ändern, wenn ich das nächste Mal öffne app das array bekommt die neuen werte von anfang an.

Ich habe diesen Code ausprobiert und nun läuft meine App, aber nach dem Herunterfahren sind alle Werte in "iss [] []" wieder gleich 0 und nicht die Werte, die ich gespeichert habe.

int[][] iss = new int [11][11];
String name;


public void readFile2(String fileName) {
SharedPreferences prefs = getPreferences(MODE_PRIVATE);

String savedString = prefs.getString(fileName, "");
StringTokenizer st = new StringTokenizer(savedString, ",");
for (int i = 0; i < 11; i++) {
while (st.hasMoreTokens()) {
iss[0][i] = Integer.parseInt(st.nextToken());
}
}

String savedString1 = prefs.getString(fileName + "1", "");
StringTokenizer st1 = new StringTokenizer(savedString1, ",");
for (int i = 0; i < 11; i++) {
while (st1.hasMoreTokens()) {
iss[1][i] = Integer.parseInt(st1.nextToken());
}
}

String savedString2 = prefs.getString(fileName + "2", "");
StringTokenizer st2 = new StringTokenizer(savedString2, ",");
for (int i = 0; i < 11; i++) {
while (st2.hasMoreTokens()) {
iss[2][i] = Integer.parseInt(st2.nextToken());
}
}

String savedString3 = prefs.getString(fileName + "3", "");
StringTokenizer st3 = new StringTokenizer(savedString3, ",");
for (int i = 0; i < 11; i++) {
while (st3.hasMoreTokens()) {
iss[3][i] = Integer.parseInt(st3.nextToken());
}
}

String savedString4 = prefs.getString(fileName + "4", "");
StringTokenizer st4 = new StringTokenizer(savedString4, ",");
for (int i = 0; i < 11; i++) {
while (st4.hasMoreTokens()) {
iss[4][i] = Integer.parseInt(st4.nextToken());
}
}

String savedString5 = prefs.getString(fileName + "5", "");
StringTokenizer st5 = new StringTokenizer(savedString5, ",");
for (int i = 0; i < 11; i++) {
while (st5.hasMoreTokens()) {
iss[5][i] = Integer.parseInt(st5.nextToken());
}
}

String savedString6 = prefs.getString(fileName + "6", "");
StringTokenizer st6 = new StringTokenizer(savedString6, ",");
for (int i = 0; i < 11; i++) {
while (st6.hasMoreTokens()) {
iss[6][i] = Integer.parseInt(st6.nextToken());
}
}

String savedString7 = prefs.getString(fileName + "7", "");
StringTokenizer st7 = new StringTokenizer(savedString7, ",");
for (int i = 0; i < 11; i++) {
while (st7.hasMoreTokens()) {
iss[7][i] = Integer.parseInt(st7.nextToken());
}
}

String savedString8 = prefs.getString(fileName + "8", "");
StringTokenizer st8 = new StringTokenizer(savedString8, ",");
for (int i = 0; i < 11; i++) {
while (st8.hasMoreTokens()) {
iss[8][i] = Integer.parseInt(st8.nextToken());
}
}

String savedString9 = prefs.getString(fileName + "9", "");
StringTokenizer st9 = new StringTokenizer(savedString9, ",");
for (int i = 0; i < 11; i++) {
while (st9.hasMoreTokens()) {
iss[9][i] = Integer.parseInt(st9.nextToken());
}
}

String savedString10 = prefs.getString(fileName + "10", "");
StringTokenizer st10 = new StringTokenizer(savedString10, ",");
for (int i = 0; i < 11; i++) {
while (st10.hasMoreTokens()) {
iss[10][i] = Integer.parseInt(st10.nextToken());
}
}
}

public void firstfile(){
Bundle extras = getIntent().getExtras();
fritis = extras.getIntArray("friti");//friti is value from spinner

if((fritis[0] == 0) && (fritis[1]== 10)){
String fileName = "all";
name=fileName;

readFile2(fileName);
}
else if((fritis[0] == 0) && (fritis[1]== 0)){
String fileName = "cer";
name=fileName;

readFile2(fileName);
}
else if(fritis[0] == 1){
String fileName = "one";
name=fileName;

readFile2(fileName);
}
else if(fritis[0] == 2){
String fileName = "two";
name=fileName;

readFile2(fileName);
}
else if(fritis[0] == 3){
String fileName = "three";
name=fileName;

readFile2(fileName);
}
else if(fritis[0] == 4){
String fileName = "four";
name=fileName;

readFile2(fileName);
}
else if(fritis[0] == 5){
String fileName = "five";
name=fileName;

readFile2(fileName);
}
else if(fritis[0] == 6){
String fileName = "six";
name=fileName;

readFile2(fileName);
}
else if(fritis[0] == 7){
String fileName = "seven";
name=fileName;

readFile2(fileName);
}
else if(fritis[0] == 8){
String fileName = "eight";
name=fileName;

readFile2(fileName);
}
else if(fritis[0] == 9){
String fileName = "nine";
name=fileName;

readFile2(fileName);
}
else if(fritis[0] == 10){
String fileName = "ten";
name=fileName;

readFile2(fileName);
}


}

public void save_file(){//saves iss[][]

SharedPreferences prefs = getPreferences(MODE_PRIVATE);
StringBuilder str = new StringBuilder();
for (int i = 0; i < 11; i++) {
str.append(iss[0][i]).append(",");
}
prefs.edit().putString(name, str.toString());

StringBuilder str1 = new StringBuilder();
for (int i = 0; i < 11; i++) {
str1.append(iss[1][i]).append(",");
}
prefs.edit().putString(name+"1", str1.toString());

StringBuilder str2 = new StringBuilder();
for (int i = 0; i < 11; i++) {
str2.append(iss[2][i]).append(",");
}
prefs.edit().putString(name+"2", str2.toString());

StringBuilder str3 = new StringBuilder();
for (int i = 0; i < 11; i++) {
str3.append(iss[3][i]).append(",");
}
prefs.edit().putString(name+"3", str3.toString());

StringBuilder str4 = new StringBuilder();
for (int i = 0; i < 11; i++) {
str4.append(iss[4][i]).append(",");
}
prefs.edit().putString(name+"4", str4.toString());

StringBuilder str5 = new StringBuilder();
for (int i = 0; i < 11; i++) {
str5.append(iss[5][i]).append(",");
}
prefs.edit().putString(name+"5", str5.toString());

StringBuilder str6 = new StringBuilder();
for (int i = 0; i < 11; i++) {
str6.append(iss[6][i]).append(",");
}
prefs.edit().putString(name+"6", str6.toString());

StringBuilder str7 = new StringBuilder();
for (int i = 0; i < 11; i++) {
str7.append(iss[7][i]).append(",");
}
prefs.edit().putString(name+"7", str7.toString());

StringBuilder str8 = new StringBuilder();
for (int i = 0; i < 11; i++) {
str8.append(iss[8][i]).append(",");
}
prefs.edit().putString(name+"8", str8.toString());

StringBuilder str9 = new StringBuilder();
for (int i = 0; i < 11; i++) {
str9.append(iss[9][i]).append(",");
}
prefs.edit().putString(name+"9", str9.toString());

StringBuilder str10 = new StringBuilder();
for (int i = 0; i < 11; i++) {
str10.append(iss[10][i]).append(",");
}
prefs.edit().putString(name+"10", str10.toString());


}

/ Danke im Voraus

Antworten:

0 für die Antwort № 1

Ich empfehle den Mechanismus SharedPreferences in Android dafür zu verwenden. Überprüfen Sie diese Frage für einige Beispiele:

Ist es möglich, ein Array oder Objekt zu SharedPreferences auf Android hinzuzufügen?


Verwandte Fragen
Speisekarte