SharedPreferences Save value of Int in a TextView of another activity(SharedPreferences 将 Int 的值保存在另一个活动的 TextView 中)
问题描述
我尝试使用 SharedPreferences 保存另一个活动的 TextView 中显示的类Ring"中的 int redRing"的值,它可以工作,但不是每次都有效,如果我有时会重置我的 int 的值退出应用程序并再次返回我的应用程序,但并非每次都如此.如果有人可以尝试告诉我我的代码有什么问题,我做了一个简单的代码小示例?
I try to save the value of an int "redRing" from the class "Ring" displayed in a TextView of another activity using SharedPreferences, it can work but not everytime, sometimes there's a reset of the value of my int if I quit the app and come back again in my app but not everytime. I did a simple small example of code if someone can try to say me what's wrong with my code?
1) 环类:
public class Ring { // User search rings with different colors
public static int color;
public static int redRing, greenRing, nbRing, somment, someGreen, someRing;
public static TextView tvNbRing, tvRingColor;
// Constuctor
public Ring(int c) { this.color = c; }
// Getter
public int getColor() { return this.color; }
public void setColor(int dice) { // For the example there’s 2 colors
dice = (int)(Math.random()*2+1);
this.color = dice;
if(this.color == 1) { // If 1 so user find a red ring
redRing = someint + 1;
tvRingColor.setText("It's a red ring");
}
if(this.color == 2) {
greenRing = someGreen + 1; // I add the green color
tvRingColor.setText("It's a green ring");
}
}
public int setNbRingFind() { // Nb = Number
int rings = (int)(Math.random()*2+0); // User can find 0 or 1 ring
Ring r = new Ring(this.color);
if(rings>0) {
nbRing = someRing + 1;
tvNbRing.setText("You find " + rings + " ring");
r.setColor(this.color);
} else { tvNbRing.setText("Nothing find, try again"); }
return rings;
}
}
2) Activity1:主要
2) Activity1 : Main
import static fr.ringshared.Ring.tvNbRing;
import static fr.ringshared.Ring.tvRingColor;
import static fr.ringshared.Ring.redRing;
import static fr.ringshared.Ring.someint;
import static fr.save.sharedpreferences.Ring.greenRing;
import static fr.save.sharedpreferences.Ring.nbRing;
import static fr.save.sharedpreferences.Ring.someGreen;
import static fr.save.sharedpreferences.Ring.someRing;
public class MainActivity extends Activity {
Button search, stock, useRing;
TextView tvRing;
int x = 0;
Ring r = new Ring(x); // Create a Ring with a x color
private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
search = (Button) findViewById(R.id.radar);
stock = (Button) findViewById(R.id.capsules);
useRing = (Button) findViewById(R.id.useRing);
tvRing = (TextView) findViewById(R.id.ring);
tvNbRing = (TextView) findViewById(R.id.tvNbRing);
tvRingColor = (TextView) findViewById(R.id.tvRingColor);
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
someint = prefs.getInt("someint", 0);
someGreen = prefs.getInt("someGreen", 0);
someRing = prefs.getInt("someRing", 0);
tvRing.setText("Objets trouvés : " + String.valueOf(someRing));
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
someint = prefs.getInt("someint", 0);
someGreen = prefs.getInt("someGreen", 0);
someRing = prefs.getInt("someRing", 0);
addRing();
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("someint", redRing);
editor.putInt("someGreen", greenRing);
editor.putInt("someRing", bring);
editor.apply();
editor.commit();
lastNumber();
}
});
stock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentStock = new Intent(MainActivity.this, StockActivity.class);
startActivity(intentStock);
}
});
}
public void addRing() { // I use a method from the class Ring
r.setNbRingFind();
}
public void lastNumber(){
prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
someint = prefs.getInt("someint", 0);
someGreen = prefs.getInt("someGreen", 0);
someRing = prefs.getInt("someRing", 0);
tvRing.setText("Objects find : " + String.valueOf(someint));
}
}
3) Activity2 : StockActivity
3) Activity2 : StockActivity
import static fr.ringshared.Ring.someint;
import static fr.save.sharedpreferences.Ring.someGreen;
import static fr.save.sharedpreferences.Ring.someRing;
public class StockActivity extends Activity {
TextView tvRing2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_capsules);
tvRing2 = (TextView) findViewById(R.id.ring2);
tvGreen = (TextView) findViewById(R.id.green);
tvTotal = (TextView) findViewById(R.id.total);
tvRing2.setText("Red rings find : " + String.valueOf(someint));
tvGreen.setText("Green rings find " + String.valueOf(someGreen));
tvTotal.setText("All rings find : " + String.valueOf(someRing));
}
}
如果有人可以帮助我找到我的代码有问题的地方,因为在逻辑上没有错误显示,只是在运行期间,有时 int "redRing" 或 "someint" 的值已重置为 0 而不是保留最后一个数字他的价值.
If someone can help me to find where my code has a problem because in logical no error show, just during running, sometimes the value of the int "redRing" or "someint" has resetted to 0 instead of keep the last number of his value.
推荐答案
getSharedPreferences(String name, int mode)
从上下文调用.如果您使用来自活动 SharedPreferences 的上下文,则可能找不到键.你可以试试 getApplicationContext().getSharedPreferences(String name, int mode)
getSharedPreferences(String name, int mode)
call from a Context. If you use context from activity SharedPreferences maybe not found key. You can try getApplicationContext().getSharedPreferences(String name, int mode)
这篇关于SharedPreferences 将 Int 的值保存在另一个活动的 TextView 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SharedPreferences 将 Int 的值保存在另一个活动的 TextView 中
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01