我有一个应用程序可以跟踪吸入器等药物的医疗库存水平。我有一张吸入器的图片,然后有一个变量,其中包含剩余的剂量。其他吸入器和片剂也是如此。用户可以单击“减号”按钮,这将使剂量/片剂的量减少1。
我正在尝试保存用户离开屏幕的数字,例如,如果他们将剂量从200减少到150,我希望150显示用户何时再次进入该活动。
我采用的方法是使用“共享首选项”,但是在测试中它没有用,药物级别仅显示了OnCreate()方法中的设置。
这是我的代码:
public class ManageMedication extends AppCompatActivity {
Button minusblue;
Button minus2;
Button minus3;
Button minus4;
int inhalerlevelblue = 200;
int inhalerlevelbrown = 200;
int inhalerlevelred = 120;
int tabletlevel1 = 50;
int tabletlevel2 = 50;
int levelshowninhaler2;
int newlevel1;
int newlevel2;
int newlevel3;
int newlevel4;
String shownewlevel1;
String shownewlevel2;
String shownewlevel3;
String shownewlevel4;
TextView displaylevel;
TextView displaylevel2;
TextView displaylevel3;
TextView displaylevel4;
String showInhalerLevel;
String showInhalerLevel2;
String showtabletLevel2;
String showtabletLevel1;
String levelbrown;
String levelred;
Spinner spinnerInhalers;
ImageView imageView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.managemedication);
minusblue = findViewById(R.id.buttonminus);
minus2 = findViewById(R.id.buttonminus2);
minus3 = findViewById(R.id.buttonminus3);
minus4 = findViewById(R.id.buttonminus4);
showInhalerLevel = String.valueOf(inhalerlevelblue);
showInhalerLevel2 = String.valueOf(levelshowninhaler2);
showtabletLevel1 = String.valueOf(tabletlevel1);
showtabletLevel2 = String.valueOf(tabletlevel2);
levelbrown = String.valueOf(inhalerlevelbrown);
levelred = String.valueOf(inhalerlevelred);
spinnerInhalers = findViewById(R.id.spinnerInhalers);
displaylevel = findViewById(R.id.textViewStockBlue);
displaylevel2 = findViewById(R.id.textViewStock2);
displaylevel3 = findViewById(R.id.textViewStock3);
displaylevel4 = findViewById(R.id.textViewStock4);
imageView2 = findViewById(R.id.imageView2);
displaylevel.setText(showInhalerLevel);
displaylevel3.setText(showtabletLevel1);
displaylevel4.setText(showtabletLevel2);
minusblue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
inhalerlevelblue = inhalerlevelblue - 1;
newlevel1 = inhalerlevelblue - 1;
shownewlevel1 = String.valueOf(newlevel1);
displaylevel.setText(shownewlevel1);
}
});
minus2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
levelshowninhaler2 = levelshowninhaler2 - 1;
newlevel2 = levelshowninhaler2 - 1;
shownewlevel2 = String.valueOf(newlevel2);
displaylevel2.setText(shownewlevel2);
}
});
minus3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tabletlevel1 = tabletlevel1 - 1;
newlevel3 = tabletlevel1 - 1;
shownewlevel3 = String.valueOf(newlevel3);
displaylevel3.setText(shownewlevel3);
}
});
minus4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tabletlevel2 = tabletlevel2 - 1;
newlevel4 = tabletlevel2 - 1;
shownewlevel4 = String.valueOf(newlevel4);
displaylevel4.setText(shownewlevel4);
}
});
final ArrayAdapter<String> myAdapter2 = new ArrayAdapter<>(ManageMedication.this,
android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.inhalers));
myAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerInhalers.setAdapter(myAdapter2);
spinnerInhalers.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position2, long l) {
switch (position2) {
case 0:
imageView2.setImageResource(R.drawable.browninhaler);
displaylevel2.setText(levelbrown);
levelshowninhaler2 = inhalerlevelbrown;
break;
case 1:
imageView2.setImageResource(R.drawable.redinhaler);
displaylevel2.setText(levelred);
levelshowninhaler2 = inhalerlevelred;
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
String[] list = getResources().getStringArray(R.array.inhalers);
spinnerInhalers.setSelection(Integer.parseInt(list[0]));
}
});
}
@Override
protected void onPause(){
super.onPause();
SharedPreferences.Editor editor = getSharedPreferences("myprefs", MODE_PRIVATE).edit();
editor.putInt("newlevel1", newlevel1);
editor.putInt("newlevel2", newlevel2);
editor.putInt("newlevel3", newlevel3);
editor.putInt("newlevel4", newlevel4);
editor.putString("shownewlevel1", shownewlevel1);
editor.putString("shownewlevel2", shownewlevel2);
editor.putString("shownewlevel3", shownewlevel3);
editor.putString("shownewlevel4", shownewlevel4);
editor.apply();
}
@Override
protected void onResume(){
super.onResume();
SharedPreferences prefs = getSharedPreferences("myprefs", MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
int retrieved1 = prefs.getInt("newlevel1", 0);//0 is the default value.
int retrieved2 = prefs.getInt("newlevel2", 0);
int retrieved3 = prefs.getInt("newlevel3", 0);
int retrieved4 = prefs.getInt("newlevel4", 0);
String retrievedlevel1 = prefs.getString("shownewlevel1", "No name defined");//"No name defined" is the default value.
String retrievedlevel2 = prefs.getString("shownewlevel2", "No name defined");
String retrievedlevel3 = prefs.getString("shownewlevel3", "No name defined");
String retrievedlevel4 = prefs.getString("shownewlevel4", "No name defined");
displaylevel.setText(retrievedlevel1);
displaylevel2.setText(retrievedlevel2);
displaylevel3.setText(retrievedlevel3);
displaylevel4.setText(retrievedlevel4);
}
}
@Override
protected void onDestroy(){
super.onDestroy();
SharedPreferences.Editor editor = getSharedPreferences("myprefs", MODE_PRIVATE).edit();
editor.putInt("newlevel1", newlevel1);
editor.putInt("newlevel2", newlevel2);
editor.putInt("newlevel3", newlevel3);
editor.putInt("newlevel4", newlevel4);
editor.putString("shownewlevel1", shownewlevel1);
editor.putString("shownewlevel2", shownewlevel2);
editor.putString("shownewlevel3", shownewlevel3);
editor.putString("shownewlevel4", shownewlevel4);
editor.apply();
}
}
如您所见,我正在尝试将新值和该值的字符串存储到“共享首选项”中,并再次在OnResume()中显示它们。
这是正确的方法还是有人可以帮助我?
谢谢