Home > database >  Java code to show action on x amount of presses?
Java code to show action on x amount of presses?

Time:11-09

I have an iOS app that I worked on some time ago, and the function it uses I like, and would like to somehow get it to work on some Android code I have.

At the top of my MainActivity I have

SharedPreferences sharedpreferences;

public static final String MyPREFERENCES = "nShownLobby";

on my onCreate I have

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

And I am calling this method

changeTextButton.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View v){
        long nb_shown_lobby = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).getLong("nShownLobby", 0); 
          nb_shown_lobby; 
        if ((nb_shown_lobby % 20) == 0) { 
            this.turnAround(); 
        }
        int random = (int) (Math.random() * manyDifferentStrings.length);
        if (random == oldVaue) {
            random = (int) (Math.random() * manyDifferentStrings.length);
        }
        changingText.setText(manyDifferentStrings[random]);
        oldVaue = random;
        try {
            mySound.start();
        } catch (NullPointerException e) {
            mySound = MediaPlayer.create(MainActivity.this, R.raw.sound);
        }
    }

    private void turnAround() {
        //Do something here
        Log.i("Do Something ", "");
    }
});

The intention is that after every 20 presses, the turnAround() method is called but it does not... It just gets ignored - I am guessing I have missed something?

CodePudding user response:

**According to your question your code should be like this**

private SharedPreferences sharedpreferences;
private SharedPreferences.Editor editor;
private static final String MyPREFERENCES = "nShownLobby";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    editor = sharedpreferences.edit();

    changeTextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){


            int nb_shown_lobby = sharedpreferences.getInt("nShownLobby", 0);
            if ((nb_shown_lobby % 20) == 0) {
                turnAround();
            }

            nb_shown_lobby  = 1;
            editor.putInt("nShownLobby", nb_shown_lobby);
            editor.commit();

            int random = (int) (Math.random() * manyDifferentStrings.length);
            if (random == oldVaue) {
                random = (int) (Math.random() * manyDifferentStrings.length);
            }
            changingText.setText(manyDifferentStrings[random]);
            oldVaue = random;

            try {
                mySound.start();
            } catch (NullPointerException e) {
                mySound = MediaPlayer.create(MainActivity.this, R.raw.sound);

            }

        }

    });


}

//created this in activity, not in the button onclick
private void turnAround() {

    //Do something here
    Log.i("Do Something ", "");
}
  • Related