我正在使用共享首选项编辑器存储数字以便在我的项目中保存和使用另一个类,这是我保存数据的来源:
public void overallEditor(){
SharedPreferences.Editor editor = someData.edit();
editor.putInt("winsOverall", winsOverall);
editor.putInt("losesOverall", losesOverall);
editor.putInt("tiesOverall", tiesOverall);
editor.commit();
}
这是我检索数据的来源:
private void getStats() {
// TODO Auto-generated method stub
SharedPreferences someData = getSharedPreferences(filename, 0);
bestStreakStatsi = someData.getInt("newBestStreak", 0);
winsOverall = someData.getInt("winsOverall", 0);
tiesOverall = someData.getInt("tiesOverall", 0);
losesOverall = someData.getInt("losesOverall", 0);
}
这是我的第一堂课的来源,如果您需要更多来自我的来源,我可以提供它,但我删除了无关紧要的内容:
int wins, ties, loses, choice, streak, bestStreak, z, winsOverall, losesOverall, tiesOverall;
private void refresh(){
winrep.setText(w + wins);
loserep.setText(l + loses);
tierep.setText(t + ties);
SharedPreferences.Editor editor = someData.edit();
editor.putInt("winsp", wins);
editor.putInt("losesp", loses);
editor.putInt("tiesp", ties);
editor.putInt("streakp", streak);
editor.commit();
overallEditor();
}
public void overallEditor(){
SharedPreferences.Editor editor = someData.edit();
editor.putInt("winsOverall", winsOverall);
editor.putInt("losesOverall", losesOverall);
editor.putInt("tiesOverall", tiesOverall);
editor.commit();
}
这是应该检索整个内容的地方:
public class stats extends Activity{
public static String filename = "stats";
TextView bestStreakStats, overallWins, overallLoses, overallTies;
int bestStreakStatsi, winsOverall, losesOverall, tiesOverall;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.stats);
initizialize();
getStats();
displayStats();
}
private void displayStats() {
// TODO Auto-generated method stub
bestStreakStats.setText("You best winning streak: \t" + bestStreakStatsi);
overallWins.setText("Your overall wins: \t" + overallWins);
overallLoses.setText("Your overall loses: \t" + overallLoses);
overallTies.setText("Your overall ties: \t" + overallTies);
}
private void getStats() {
// TODO Auto-generated method stub
SharedPreferences someData = getSharedPreferences(filename, 0);
bestStreakStatsi = someData.getInt("newBestStreak", 0);
winsOverall = someData.getInt("winsOverall", 0);
tiesOverall = someData.getInt("tiesOverall", 0);
losesOverall = someData.getInt("losesOverall", 0);
}
private void initizialize() {
// TODO Auto-generated method stub
bestStreakStats = (TextView) findViewById(R.id.tvBestStreakStats);
overallWins = (TextView) findViewById(R.id.tvOverallWins);
overallLoses = (TextView) findViewById(R.id.tvOverallLoses);
overallTies = (TextView) findViewById(R.id.tvOverallTies);
}
}
答案 0 :(得分:0)
发生这种情况的原因很可能是因为你正在这样做
[TextView Object].getText();
这是不正确的,它应该是:
[TextView Object].getText().toString();
您真的需要修复您的命名约定。发生这种情况的原因是因为你在displayStats()方法中显示了错误的对象。
对于一些建议,我建议您使用TextViews的命名约定
TextView tx_[somename];
和你的价值
int value_[somename]
答案 1 :(得分:0)
我认为您的问题是您引用了错误的变量。你是说。
overallWins.setText("Your overall wins: \t" + overallWins);
但我认为应该是:
overallWins.setText("Your overall wins: \t" + winsOverall);
.setText
的其余部分也是如此。看看是否有帮助。