进入循环时,SharedPreferences数据成为默认值

时间:2019-06-25 23:08:59

标签: java android sharedpreferences

我有一个可以正常工作的SharedPreferences代码,但是当我尝试用Views初始化数据时,它突然变成了默认值,仅用于布尔数组spLayoutV []和spBakiV []。其他人就像魅力一样。

    retrieveData();
    int n;
    for(n = 0; n < 17 ; n ++){
        i = n + 1;
        TextView tvWarna = findViewById(getResources().getIdentifier("warna" + i, "id", MainActivity.this.getPackageName()));
        TextView tvWarnaBaki = findViewById(getResources().getIdentifier("warnaBaki" + i, "id", MainActivity.this.getPackageName()));
        TextView tvMenstruasi = findViewById(getResources().getIdentifier("menstruasi" + i, "id", MainActivity.this.getPackageName()));
        TextView tvMenstruasiBaki = findViewById(getResources().getIdentifier("menstruasiBaki" + i, "id", MainActivity.this.getPackageName()));
        TextView tvStatus = findViewById(getResources().getIdentifier("status" + i, "id", MainActivity.this.getPackageName()));
        TextView tvStatusBaki = findViewById(getResources().getIdentifier("statusBaki" + i, "id", MainActivity.this.getPackageName()));
        TextView tvTempoh = findViewById(getResources().getIdentifier("tempoh" + i, "id", MainActivity.this.getPackageName()));
        TextView tvTempohBaki = findViewById(getResources().getIdentifier("tempohBaki" + i, "id", MainActivity.this.getPackageName()));
        LinearLayout layoutHari = findViewById(getResources().getIdentifier("layoutHari" + i, "id", MainActivity.this.getPackageName()));
        LinearLayout layoutBaki = findViewById(getResources().getIdentifier("layoutBaki" + i, "id", MainActivity.this.getPackageName()));

        if(MainActivity.this.spLayoutV[n]){
            layoutHari.setVisibility(View.VISIBLE);
        } else {
            layoutHari.setVisibility(View.GONE);
        }

        if(MainActivity.this.spBakiV[n]){
            layoutBaki.setVisibility(View.VISIBLE);
        } else {
            layoutBaki.setVisibility(View.GONE);
        }
        Log.v("hari initialize", "" + spLayoutV[n]);
        Log.v("baki initialize", "" + spBakiV[n]);

        tvWarna.setText(spWarnaDarah[n]);
        tvWarnaBaki.setText(spWarnaDarahBaki[n]);
        tvMenstruasi.setText(spMenstruasi[n]);
        tvMenstruasiBaki.setText(spMenstruasiBaki[n]);
        tvTempoh.setText(spTempoh[n]);
        tvTempohBaki.setText(spTempohBaki[n]);
        tvStatus.setText(spStatus[n]);
        tvStatusBaki.setText(spStatusBaki[n]);
    }



//toSaveData
public void saveData(){
    SharedPreferences prefs = getApplicationContext().getSharedPreferences("prefs files", 0);
    SharedPreferences.Editor editor = prefs.edit();
    int n;
    for(n = 0; n < 17; n++) {
        i = n + 1;
        TextView tvWarna = findViewById(getResources().getIdentifier("warna" + i, "id", MainActivity.this.getPackageName()));
        TextView tvWarnaBaki = findViewById(getResources().getIdentifier("warnaBaki" + i, "id", MainActivity.this.getPackageName()));
        TextView tvMenstruasi = findViewById(getResources().getIdentifier("menstruasi" + i, "id", MainActivity.this.getPackageName()));
        TextView tvMenstruasiBaki = findViewById(getResources().getIdentifier("menstruasiBaki" + i, "id", MainActivity.this.getPackageName()));
        TextView tvStatus = findViewById(getResources().getIdentifier("status" + i, "id", MainActivity.this.getPackageName()));
        TextView tvStatusBaki = findViewById(getResources().getIdentifier("statusBaki" + i, "id", MainActivity.this.getPackageName()));
        TextView tvTempoh = findViewById(getResources().getIdentifier("tempoh" + i, "id", MainActivity.this.getPackageName()));
        TextView tvTempohBaki = findViewById(getResources().getIdentifier("tempohBaki" + i, "id", MainActivity.this.getPackageName()));
        LinearLayout layoutHari = findViewById(getResources().getIdentifier("layoutHari" + i, "id", MainActivity.this.getPackageName()));
        LinearLayout layoutBaki = findViewById(getResources().getIdentifier("layoutBaki" + i, "id", MainActivity.this.getPackageName()));

        editor.putString("menstruasi" + n, tvMenstruasi.getText().toString());
        editor.putString("menstruasiBaki" + n, tvMenstruasiBaki.getText().toString());
        editor.putString("tempoh" + n, tvTempoh.getText().toString());
        editor.putString("tempohBaki" + n, tvTempohBaki.getText().toString());
        editor.putString("warna" + n, tvWarna.getText().toString());
        editor.putString("warnaBaki" + n, tvWarnaBaki.getText().toString());
        editor.putString("status"+ n, tvStatus.getText().toString());
        editor.putString("statusBaki" + n, tvStatusBaki.getText().toString());


        Toast.makeText(this, "Data Saved", Toast.LENGTH_SHORT).show();
    }

    for(i = 0; i < 17; i++) {
        LinearLayout layoutHari = findViewById(getResources().getIdentifier("layoutHari" + (i + 1), "id", MainActivity.this.getPackageName()));
        LinearLayout layoutBaki = findViewById(getResources().getIdentifier("layoutBaki" + (i + 1), "id", MainActivity.this.getPackageName()));
        boolean hariV = false;
        if (layoutHari.getVisibility() == View.VISIBLE) {
            hariV = true;
        } else {
            hariV = false;
        }

        boolean bakiV = false;
        if (layoutBaki.getVisibility() == View.VISIBLE) {
            bakiV = true;
        } else {
            bakiV = false;
        }

        editor.putBoolean("hariV" + i, hariV);
        editor.putBoolean("bakiV" + i, bakiV);
    }

    editor.apply();
}

在retrieveData()上,我实际上检查了日志,看看数据是如何完成的,没有问题,但是随后突然进入for循环时,布尔数组全部变为默认的false值。我不明白为什么,请帮忙。预先感谢。

0 个答案:

没有答案