您好我正在使用app android注意到卡牌游戏的得分。 我正在寻找如何保存实际比分和团队的能力。当应用程序被破坏或停止时命名,以便在我再次打开它时找到它们。
public class ShowScore extends AppCompatActivity {
SharedPreferences save = null;
private TextView nameTeam1= null;
private TextView nameTeam2= null;
private TextView scoreTeam1 = null;
private TextView scoreTeam2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_score);
nameTeam1= (TextView) findViewById(R.id.team1);
nameTeam2= (TextView) findViewById(R.id.team2);
scoreTeam1 = (TextView) findViewById(R.id.score1);
scoreTeam2 = (TextView) findViewById(R.id.score2);
nameTeam1.setText(save.getString("Team1","Team 1"));
nameTeam2.setText(save.getString("Team2","Team 2"));
scoreTeam1.setText(String.valueOf(save.getInt("Score1", 0)));
scoreTeam2.setText(String.valueOf(save.getInt("Score2", 0)));
}
@Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
save = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = save.edit();
editor.putString("Team1", nameTeam1.getText().toString());
editor.putString("Team2", nameTeam2.getText().toString());
editor.putInt("Score1", Integer.parseInt(scoreTeam1.getText().toString()));
editor.putInt("Score2", Integer.parseInt(scoreTeam2.getText().toString()));
editor.commit();
}
}
编辑我应该添加修复 save = getApplicationContext()。getSharedPreferences(" MyPref",0); 进入onCreate()。
答案 0 :(得分:0)
最近我开始投资sqlite数据库,纯粹是因为它停留在系统上,并且可以进一步导出。但我遵循的一个很棒的教程是
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
这具有用于数据库应用的所有C.R.U.D。
答案 1 :(得分:0)
如果数据很小,您可以尝试多种选项,只需变量即可尝试共享首选项
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.commit(); // commit changes
就像这样,值将被存储 然后你需要
来获取数据pref.getString("key_name", null); // getting String