简体   繁体   中英

How to save state of game

I am trying to get my app to save when closed down or if back button is pressed. I'd like for it to remember which boxes are checked and what the score is on.

I've tried looking through previous articles to save the checkbox state to shared preferences but it doesn't seem to do anything. I've tried at least 5 different ways that I've found on here but it either just crashes the app or does nothing at all.

This is the code I've got:

public class levelOneActivity extends AppCompatActivity {

    TextView scoreTextView;
    Button backButton;
    ConstraintLayout pictureConstraint;
    Button nextLevelButton;
    ListView levelOneListView;
    ArrayAdapter arrayAdapter;
    int score;
    public static final String PREFS_NAME = "MyPrefsFile";
   // SharedPreferences sharedPrefs = context.getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE);



    public void backButton(View view) {
        pictureConstraint.setVisibility(View.INVISIBLE);
    }

    public void nextLevelButton(View view) {
        Log.i("info", "next level!");
        Intent intent = new Intent(getApplicationContext(), levelTwoActivity.class);
        startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level_one);
        levelOneListView = findViewById(R.id.levelOneListView);
        scoreTextView = findViewById(R.id.scoreTextView);
        pictureConstraint = findViewById(R.id.pictureConstraint);
        backButton = findViewById(R.id.backButton);
        final ImageView imageView2 = findViewById(R.id.imageView2);
        final TextView factTextView = findViewById(R.id.factTextView);
        score = 0;
        final ConstraintLayout levelFinishedConstraint = findViewById(R.id.finishedLevelConstraint);
        nextLevelButton = findViewById(R.id.nextLevelButton);

        final SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.woodlandwanderer", Context.MODE_PRIVATE);


        final String[] levelOneListList = new String[]{
                "Daisy", "Rock", "Tree", "Dandelion", "Grass"
        };

        final int[] imageList = new int[]{
                R.drawable.daisy, R.drawable.rock, R.drawable.tree, R.drawable.dandelion, R.drawable.grass
        };

        final String[] factList = new String[]{
                "Daisies are cool", "Rocks are fun to throw!", "Trees have leaves!", "Dandelions are yellow", "Grass is green"
        };


        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_checked, levelOneListList);


        levelOneListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                pictureConstraint.setVisibility(View.VISIBLE);
                imageView2.setImageResource(imageList[i]);
                factTextView.setText(factList[i]);
                return true;
            }
        });

        levelOneListView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        levelOneListView.setAdapter(arrayAdapter);


        levelOneListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Log.i("Info", "Clicked");
                CheckedTextView checkedTextView = (CheckedTextView) view;


                if (checkedTextView.isChecked()) {

                    if (score < 9) {
                        score++;
                    } else {
                        score++;
                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                levelFinishedConstraint.setVisibility(View.VISIBLE);
                            }

                        }, 1000);
                    }
                } else {
                    Log.i("Info", "Not checked");
                    score = score - 1;
                }

                scoreTextView.setText(Integer.toString(score) + "/10");
            }
        });
    }
}

I would suggest you to come up with an xml-file in which you set your own tag for check or unchecked boxes. Whenever the user exits the programm or presses the back button, the last thing before the application shut down is saving the current state of the checked boxes and replace the old version.

Another way to save the current state would be the database. I would modify it so can even type in xml-style, or you just push the names of the checked boxes to your database. Always the last step before shutting the application down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM