您好我希望我的D2 int数组从文件中获取其值。如果文件不存在我想创建它并将aray的所有值设置为0.然后当数组中的值发生变化时,我想单击我的保存按钮,以便文件中的值发生变化,以便下次打开时应用程序数组从开始获取新值。
我尝试了这段代码,现在我的应用程序运行但是在关机后,“iss [] []”中的所有值再次等于0而不是我保存的值。
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());
}
/提前谢谢
答案 0 :(得分:0)
我建议在Android中使用SharedPreferences机制。请查看此问题以获取一些示例:
Is it possible to add an array or object to SharedPreferences on Android