2

I have int value and I want it to increases by 1 when we click positive or negative button of Alert dialogue, and store the int value even when the user closes the app. I've done these but I don't know why is this not working.

int counter;

in the oncreate

initA();
private void initA(){

if(getCounter() < 1)
{makeAlertDialogOther();}
}
private void makeAlertDialogOther() {
        new AlertDialog.Builder(getActivity())
                .setMessage(Constant.SETTINGS.getEntranceMsg())
                .setCancelable(false)
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        counterU();

                    }
                })
                .setPositiveButton("Update", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        counterU();
                    }
                })
                .show();
    }

This is where I made sharepreference:

 private void counterU() {
        sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        int oldCounter = sp.getInt("counterValue", 0);
        editor.putInt("counterValue", oldCounter + 1);
        editor.apply();
    }
private int getCounter() {
        sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
        return sp.getInt("counterValue", 0);
    }
madan
  • 109
  • 1
  • 11

1 Answers1

2

The reason your code not working: Whenever you close and open screen again, you start to save a new counter value (start from 0) again to your SharedPreferences.
The solution: Whenever we start to save a counter to SharedPreferences, we get the old value of counter in SharedPreferences first then increase and save back.

private void initA() {
     if(getCounter() < 1) {
        makeAlertDialogOther();
     }
}

private void makeAlertDialogOther() {
    new AlertDialog.Builder(getActivity())
        .setMessage(Constant.SETTINGS.getEntranceMsg())
        .setCancelable(false)
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                counterU();
            }
        })
        .setPositiveButton("Update", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                counterU();
            }
        })
        .show();
}

private void counterU() {
    sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    int oldCounter = sp.getInt("counterValue", 0);
    editor.putInt("counterValue", oldCounter + 1);
    editor.apply();
}

private int getCounter() {
    sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
    return sp.getInt("counterValue", 0);
}
Linh
  • 57,942
  • 23
  • 262
  • 279
  • it didn't work. I have wrapped it with this, `if (counter < Integer.parseInt(Constant.SETTINGS.getEntranceCode())) {makeAlertDialogOther()}` where `Integer.parseInt(Constant.SETTINGS.getEntranceCode())`'s value is 1. But, when I clicked later and closed the app and again opened it, the actiondialogue is still showing – madan Jun 26 '20 at 03:16
  • oh, you mean I should replace this 'int counter = 0;` with `int counter;' in top? let me try it again – madan Jun 26 '20 at 03:19
  • yes, this is the problem, because this counter **always** = 0 whenever you close and open activity again – Linh Jun 26 '20 at 03:20
  • from your code I changed `int counter = sp.getInt("counterValue", Context.MODE_PRIVATE);` into `counter = sp.getInt("counterValue", Context.MODE_PRIVATE);` and put `int counter;` at top because i should get counter value in if condition as I commented above. But, the alert dialogue is still showing when I clicked later and close and again open the app. To test It manually i put the value of counter=1; and it is not showing the alertdialogue. But i want to increase its value when clicked in cancel or update :( – madan Jun 26 '20 at 03:29
  • I test on my device and it working as expected. I just put 1 more Log in my code (Log.i("TAG", "counter = "+counter)), can you put it in your code and test again to see the counter can increase the value or not – Linh Jun 26 '20 at 03:34
  • I've updated my question. Can you please please check it in your by exactly those code? – madan Jun 26 '20 at 03:46
  • before this line `if(counter < 1) {makeAlertDialogOther()} }`, you need to get the `couter` value from SharedPreferences – Linh Jun 26 '20 at 03:49
  • before that line, should I keep `counterU();` ? – madan Jun 26 '20 at 03:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216687/discussion-between-phan-van-linh-and-madan). – Linh Jun 26 '20 at 03:55
  • can you please help me with this question? https://stackoverflow.com/questions/62531641/try-and-catch-while-retrieving-text-form-sqlite – madan Jun 29 '20 at 13:03
  • I've updated the question, added more info. check that please. – madan Jul 02 '20 at 03:07
  • help me with this please https://stackoverflow.com/questions/62887229/cant-zoom-the-webview-under-swipe-to-refresh I don't know why am I getting this – madan Jul 14 '20 at 02:56