尝试从共享首选项中检索数据
Trying to retrieve data from a Shared Preferences
我试图从共享首选项中检索数据,并试图将其转移到我的主 activity 中,我希望它显示用户获得的分数。没有错误,它只是没有保存 best_score_number_tv 或者没有正确发送它。我希望没有太多错误:(
Main_Screen
public class Main_Screen extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
getSupportActionBar().hide();
//SETS CUSTOM FONT
TextView main_screen_titleone = (TextView)findViewById(R.id.main_screen_titleone);
TextView main_screen_titletwo = (TextView)findViewById(R.id.main_screen_titletwo);
TextView best_score_tv = (TextView)findViewById(R.id.best_score_tv);
TextView best_score_number_tv = (TextView)findViewById(R.id.best_score_number_tv);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/LemonMilk.otf");
main_screen_titleone.setTypeface(myCustomFont);
main_screen_titletwo.setTypeface(myCustomFont);
best_score_tv.setTypeface(myCustomFont);
best_score_number_tv.setTypeface(myCustomFont);
//******************;
retrieveBestScore();
}
public void startTheGame(View view){
Intent intent = new Intent(this, press_screen.class);
startActivity(intent);
}
public void retrieveBestScore(){
Intent getBestScore = getIntent();
int BEST_SCORE = getBestScore.getIntExtra("BEST_SCORE", 0);
TextView best_score_number_tv = (TextView)findViewById(R.id.best_score_number_tv);
best_score_number_tv.setText(BEST_SCORE + "");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main__screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
press_screen
public class press_screen extends ActionBarActivity {
private int time_left;
private int amountOfTapsNumber;
private int bestScore;
TextView amountOfTaps;
TextView timeLeftNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_press_screen);
getSupportActionBar().hide();
timer.start();
loadInfo();
}
//Create Timer
CountDownTimer timer = new CountDownTimer(21000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftNumber = (TextView) findViewById(R.id.time_left_number_tv);
time_left = Integer.valueOf(timeLeftNumber.getText().toString()) - 1;
timeLeftNumber.setText(time_left + "");
}
@Override
public void onFinish() {
bestScore = amountOfTapsNumber;
TextView best_score_number_tv = (TextView)findViewById(R.id.best_score_number_tv);
Intent sendBestScore = new Intent(press_screen.this, Main_Screen.class);
sendBestScore.putExtra("BEST_SCORE", bestScore);
Intent goBackToMainActivity = new Intent(press_screen.this, Main_Screen.class);
startActivity(goBackToMainActivity);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_press_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void triggerTapOnMainButton(View view) {
amountOfTaps = (TextView) findViewById(R.id.amount_of_taps);
amountOfTapsNumber = Integer.valueOf(amountOfTaps.getText().toString()) + 1;
amountOfTaps.setText(amountOfTapsNumber + "");
}
public void saveInfo(String key, int bestScore){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = sp.edit();
edit.putInt("BEST_SCORE", bestScore);
edit.commit();
}
public void loadInfo(){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
int BEST_SCORE = sp.getInt("BEST_SCORE", 0);
}
}
如果你想使用 and Intent
你应该把额外的放在你传递给的意图中
startActivity
所以
Intent goBackToMainActivity = new Intent(press_screen.this, Main_Screen.class);
goBackToMainActivity.putExtra("BEST_SCORE", bestScore);
startActivity(goBackToMainActivity);
或者您可以只保存 press_screen activity 中的乐谱
在
之后
bestScore = amountOfTapsNumber;
saveInfo("BEST_SCORE", bestScore);
此外,如果您要传递一个密钥来保存信息,您可以考虑使用它
public void saveInfo(String key, int bestScore){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = sp.edit();
edit.putInt(key, bestScore);
edit.commit();
}
并且在检索时
public int retrieveInt(String key){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
return sp.getInt(key, 0);
}
我试图从共享首选项中检索数据,并试图将其转移到我的主 activity 中,我希望它显示用户获得的分数。没有错误,它只是没有保存 best_score_number_tv 或者没有正确发送它。我希望没有太多错误:(
Main_Screen
public class Main_Screen extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
getSupportActionBar().hide();
//SETS CUSTOM FONT
TextView main_screen_titleone = (TextView)findViewById(R.id.main_screen_titleone);
TextView main_screen_titletwo = (TextView)findViewById(R.id.main_screen_titletwo);
TextView best_score_tv = (TextView)findViewById(R.id.best_score_tv);
TextView best_score_number_tv = (TextView)findViewById(R.id.best_score_number_tv);
Typeface myCustomFont = Typeface.createFromAsset(getAssets(), "fonts/LemonMilk.otf");
main_screen_titleone.setTypeface(myCustomFont);
main_screen_titletwo.setTypeface(myCustomFont);
best_score_tv.setTypeface(myCustomFont);
best_score_number_tv.setTypeface(myCustomFont);
//******************;
retrieveBestScore();
}
public void startTheGame(View view){
Intent intent = new Intent(this, press_screen.class);
startActivity(intent);
}
public void retrieveBestScore(){
Intent getBestScore = getIntent();
int BEST_SCORE = getBestScore.getIntExtra("BEST_SCORE", 0);
TextView best_score_number_tv = (TextView)findViewById(R.id.best_score_number_tv);
best_score_number_tv.setText(BEST_SCORE + "");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main__screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
press_screen
public class press_screen extends ActionBarActivity {
private int time_left;
private int amountOfTapsNumber;
private int bestScore;
TextView amountOfTaps;
TextView timeLeftNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_press_screen);
getSupportActionBar().hide();
timer.start();
loadInfo();
}
//Create Timer
CountDownTimer timer = new CountDownTimer(21000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftNumber = (TextView) findViewById(R.id.time_left_number_tv);
time_left = Integer.valueOf(timeLeftNumber.getText().toString()) - 1;
timeLeftNumber.setText(time_left + "");
}
@Override
public void onFinish() {
bestScore = amountOfTapsNumber;
TextView best_score_number_tv = (TextView)findViewById(R.id.best_score_number_tv);
Intent sendBestScore = new Intent(press_screen.this, Main_Screen.class);
sendBestScore.putExtra("BEST_SCORE", bestScore);
Intent goBackToMainActivity = new Intent(press_screen.this, Main_Screen.class);
startActivity(goBackToMainActivity);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_press_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void triggerTapOnMainButton(View view) {
amountOfTaps = (TextView) findViewById(R.id.amount_of_taps);
amountOfTapsNumber = Integer.valueOf(amountOfTaps.getText().toString()) + 1;
amountOfTaps.setText(amountOfTapsNumber + "");
}
public void saveInfo(String key, int bestScore){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = sp.edit();
edit.putInt("BEST_SCORE", bestScore);
edit.commit();
}
public void loadInfo(){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
int BEST_SCORE = sp.getInt("BEST_SCORE", 0);
}
}
如果你想使用 and Intent
你应该把额外的放在你传递给的意图中
startActivity
所以
Intent goBackToMainActivity = new Intent(press_screen.this, Main_Screen.class);
goBackToMainActivity.putExtra("BEST_SCORE", bestScore);
startActivity(goBackToMainActivity);
或者您可以只保存 press_screen activity 中的乐谱 在
之后 bestScore = amountOfTapsNumber;
saveInfo("BEST_SCORE", bestScore);
此外,如果您要传递一个密钥来保存信息,您可以考虑使用它
public void saveInfo(String key, int bestScore){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = sp.edit();
edit.putInt(key, bestScore);
edit.commit();
}
并且在检索时
public int retrieveInt(String key){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
return sp.getInt(key, 0);
}