尝试从共享首选项中检索数据

时间:2015-03-14 19:11:11

标签: android android-intent sharedpreferences

我曾尝试从共享偏好设备中检索数据,并尝试将其转移到我的主要活动,我希望它显示用户获得的分数。没有错误,它只是没有保存 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);
    }
}

1 个答案:

答案 0 :(得分:2)

如果您想使用和Intent,您应该将额外的内容放在您传递给的意图中 startActivity

所以

        Intent goBackToMainActivity = new Intent(press_screen.this, Main_Screen.class);
        goBackToMainActivity.putExtra("BEST_SCORE", bestScore);
        startActivity(goBackToMainActivity);

或者,您只需保存press_screen活动的分数即可 之后

        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);
}