退出应用程序时,存储在Sharedpreference中的数据会丢失

时间:2017-08-22 09:57:54

标签: javascript java android

我正在使用计步器应用程序。步骤逐渐增加,我想在共享首选项中保存步骤数据,但是当我返回主页并重新启动应用程序时,我认为它应该保存在步骤数据中sharedprefernce刚刚消失并重置为0。 以下是我的代码的一部分。我该如何解决这个问题?谢谢。

public class MainActivity extends AppCompatActivity {
    String finalstep="";
    String valuestring1= null;

    private static final String step = "STEP";

    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf =new SimpleDateFormat("dd");
    String strDate = sdf.format(c.getTime());
    int date = Integer.parseInt(strDate);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void readData(){
        settings = getSharedPreferences(step,MODE_WORLD_READABLE);
        textSteps.setText(settings.getString(valuestring1, ""));
    }

    public void resetSteps(View v) {
        String strDate1 = sdf.format(c.getTime());
        int date1 = Integer.parseInt(strDate1);
        if(date1==date) {
            readData();
            textSteps.setText(valuestring1);
        }
        else{
            date=date1;
            numSteps = 0;
            textSteps.setText(String.valueOf(numSteps));
        }
    }
    public void saveData(){
        String s = String.valueOf(finalstep);
        settings = getSharedPreferences(step,0);
        settings.edit()
                .putString(s, valuestring1).commit();
    }

    public void pressed (View v) {
        if(v.getId()==R.id.exit) {
  saveData();
        }
    }
}

3 个答案:

答案 0 :(得分:2)

在saveData中为sharedpreference添加值时,您必须将密钥作为

传递
settings.edit().putString("KEY_STEP", s).commit();

并且在获取共享偏好值时,您必须获取

settings.getString("KEY_STEP", "")

答案 1 :(得分:1)

你犯了一些错误。你放置的钥匙是错误的位置。还使sharedPreference成为成员类

public class Main3Activity extends AppCompatActivity {

String finalstep="";
String valuestring1= "STEP_VALUE";

private static final String step = "STEP";

Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd");
String strDate = sdf.format(c.getTime());
int date = Integer.parseInt(strDate);

private SharedPreferences settings;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    settings = getSharedPreferences(step, Context.MODE_PRIVATE);
}

public void readData(){
    textSteps.setText(settings.getString(valuestring1, ""));
}

public void resetSteps(View v) {
    String strDate1 = sdf.format(c.getTime());
    int date1 = Integer.parseInt(strDate1);
    if(date1==date) {
        readData();
        textSteps.setText(valuestring1);
    }
    else{
        date=date1;
        numSteps = 0;
        textSteps.setText(String.valueOf(numSteps));
    }
}
public void saveData(){
    String s = String.valueOf(finalstep);
    settings.edit().putString(valuestring1, s).apply();
}

public void pressed (View v) {
    if(v.getId() == R.id.exit) {
        saveData();
    }
}

}

答案 2 :(得分:0)

请找到带有亮点的更新代码: -

public class MainActivity extends AppCompatActivity {
 String finalstep = "abc";/* assign some value*/
 String valuestring1 = null;

 private static final String step = "STEP";

 Calendar c = Calendar.getInstance();
 SimpleDateFormat sdf = new SimpleDateFormat("dd");
 String strDate = sdf.format(c.getTime());
 int date = Integer.parseInt(strDate);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void readData() {
    settings = getSharedPreferences(step, Context.MODE_PRIVATE);
  /*
  accessing with same value which was used at time of saving i.e. s
   */

    textSteps.setText(settings.getString(finalstep, ""));//

}

public void resetSteps(View v) {
    String strDate1 = sdf.format(c.getTime());
    int date1 = Integer.parseInt(strDate1);
    if (date1 == date) {
        readData();
        textSteps.setText(valuestring1);
    } else {
        date = date1;
        numSteps = 0;
        textSteps.setText(String.valueOf(numSteps));
    }
}

public void saveData() {
    String s = String.valueOf(finalstep);/* you can directly use finalstep no need of conversion and also no need to declare locally directly use finalstep*/
    settings = getSharedPreferences(step, 0);
    settings.edit()
            .putString(s, valuestring1).commit();
}

public void pressed(View v) {
    if (v.getId() == R.id.exit) {
        saveData();
    }
}

}

因为您正在将值存储在其他键中并尝试使用其他键进行查找。

其次,我没有得到您想要存储的内容,您想要存储的内容只是将该值分配给valuestring1,因为 null 存储在valuestring1中并且您正试图存储然后把它拿回来。