I'm sorry, but are you sure you're using SharedPreferences correctly?package com.example.andrey;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
Context context = MainActivity.this;
SharedPreferences prefer;
Editor editor;
EditText gymres;
TextView resText, resText2, resText3, resText4, resText5;
int num = 0;
Button ok;
double res, res2, res3, res4, res5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Назначаем SharedPreferences
prefer = getSharedPreferences("STORAGE_NAME", Context.MODE_PRIVATE);
editor = prefer.edit();
gymres = (EditText) findViewById(R.id.gymres);
ok = (Button) findViewById(R.id.ok);
resText = (TextView) findViewById(R.id.text1);
resText2 = (TextView) findViewById(R.id.text2);
resText3 = (TextView) findViewById(R.id.text3);
resText4 = (TextView) findViewById(R.id.text4);
resText5 = (TextView) findViewById(R.id.text5);
public void onButtonClick2(View v) {
num = Integer.parseInt(gymres.getText().toString());
res = num * 0.3;
res2 = num * 0.4;
res3 = num * 0.5;
res4 = num * 0.6;
res5 = num * 0.6;
resText.setText(Integer.toString((int) res));
resText2.setText(Integer.toString((int) res2));
resText3.setText(Integer.toString((int) res3));
resText4.setText(Integer.toString((int) res4));
resText5.setText(Integer.toString((int) res5));
//Кладём и сохраняем значения
editor.putInt("RES", (int) res);
editor.putInt("RES2", (int) res2);
editor.putInt("RES3", (int) res3);
editor.putInt("RES4", (int) res4);
editor.putInt("RES5", (int) res5);
editor.apply();
};
};
}
I've been able to find the elements of the interface from the method because I doubt it's necessary to make every compression.Here we go. res = prefer.getInt("RES", 0);
res1 = prefer.getInt("RES1", 0);
res2 = prefer.getInt("RES2", 0);
res3 = prefer.getInt("RES3", 0);
res4 = prefer.getInt("RES4", 0);
res5 = prefer.getInt("RES5", 0);
Supplement (just sign everything)I think breaking the code to individual methods will sometimes solve only aesthetic problems. So if it doesn't work like that, it won't work any other way. I'd better write it up.0.0. Require the variables necessary to maintain and obtain data:SharedPreferences prefer;
Editor editor;
final String STORAGE_NAME = "STORAGE_NAME", RES_NAME = "res", RES2_NAME = "res2", RES3_NAME = "res3", RES4_NAME = "res4", RES5_NAME = "res5";
int old_res, old_res2, old_res3, old_res4, old_res5;
double res, res2, res3, res4, res5;
0.1. We'll give them meaning:prefer = getSharedPreferences(STORAGE_NAME, Context.MODE_PRIVATE);
editor = prefer.edit();
1.1. Conserved values://Пусть это будет метод saveData
void saveData() {
editor.putInt(RES_NAME, (int) res);
editor.putInt(RES2_NAME, (int) res2);
editor.putInt(RES3_NAME, (int) res3);
editor.putInt(RES4_NAME, (int) res4);
editor.putInt(RES5_NAME, (int) res5);
editor.apply();
};
//ВНИМАНИЕ! Убедитесь, что вы утвердили изменения (написали «editor.apply()»)
1.2. We get the necessary data://Пусть это будет метод getData
void getData() {
old_res = prefer.getInt(RES_NAME, 0);
old_res1 = prefer.getInt(RES2_NAME, 0);
old_res3 = prefer.getInt(RES3_NAME, 0);
old_res4 = prefer.getInt(RES4_NAME, 0);
old_res5 = prefer.getInt(RES5_NAME, 0);
};
Now how to put them in the code. I only described "carcas," I''ll bring the code of ready activity below, but I assumed all work should be on Click.package com.example.andrey;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
public class MainActivity extends Activity {
SharedPreferences prefer;
Editor editor;
String value;
final String STORAGE_NAME = "STORAGE_NAME", RES_NAME = "res", RES2_NAME = "res2", RES3_NAME = "res3", RES4_NAME = "res4", RES5_NAME = "res5";
int old_res, old_res2, old_res3, old_res4, old_res5;
double res, res2, res3, res4, res5;
EditText gymres;
TextView resText, resText2, resText3, resText4, resText5;
int num = 0;
Button ok;
void saveData() {
editor.putInt(RES_NAME, (int) res);
editor.putInt(RES2_NAME, (int) res2);
editor.putInt(RES3_NAME, (int) res3);
editor.putInt(RES4_NAME, (int) res4);
editor.putInt(RES5_NAME, (int) res5);
editor.apply();
resText.setText(Integer.toString((int) res));
resText2.setText(Integer.toString((int) res2));
resText3.setText(Integer.toString((int) res3));
resText4.setText(Integer.toString((int) res4));
resText5.setText(Integer.toString((int) res5));
};
void getData() {
old_res = prefer.getInt(RES_NAME, 0);
old_res2 = prefer.getInt(RES2_NAME, 0);
old_res3 = prefer.getInt(RES3_NAME, 0);
old_res4 = prefer.getInt(RES4_NAME, 0);
old_res5 = prefer.getInt(RES5_NAME, 0);
resText.setText(Integer.toString(old_res));
resText2.setText(Integer.toString(old_res2));
resText3.setText(Integer.toString(old_res3));
resText4.setText(Integer.toString(old_res4));
resText5.setText(Integer.toString(old_res5));
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gymres = (EditText) findViewById(R.id.gymres);
ok = (Button) findViewById(R.id.ok);
resText = (TextView) findViewById(R.id.text1);
resText2 = (TextView) findViewById(R.id.text2);
resText3 = (TextView) findViewById(R.id.text3);
resText4 = (TextView) findViewById(R.id.text4);
resText5 = (TextView) findViewById(R.id.text5);
prefer = getSharedPreferences(STORAGE_NAME, Context.MODE_PRIVATE);
editor = prefer.edit();
getData();
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((value = gymres.getText().toString()).length() > 0) {
num = Integer.parseInt(value);
res = num * 0.3;
res2 = num * 0.4;
res3 = num * 0.5;
res4 = num * 0.6;
res5 = num * 0.6;
saveData();
};
};
});
};
}
And the makeup if something doesn't work out.<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >
<EditText
android:id="@+id/gymres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout >
I checked this code, it's full-time.