Manasi
Manasi

Reputation: 348

alertDialog accessing value inside onClick()

how to access value accepted from user in onClick() outside the onClick(). I have declared the variable currentIntervalchoice globally in which I am retriving the value. how to access the value of currentIntervalchoice in else{} part.

    private void doFirstRun()
{
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    if (settings.getBoolean("isFirstRun", true))
    {
        //-------------------------------------------------------------------------
        Toast.makeText(getApplicationContext(),"in 1st run true", Toast.LENGTH_LONG).show();
        LayoutInflater li = LayoutInflater.from(this);
        View promptsView = li.inflate(R.layout.prompts, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        // set prompts.xml to alertdialog builder
        alertDialogBuilder.setView(promptsView);
        final EditText userInput = (EditText) promptsView
                .findViewById(R.id.editTextDialogUserInput);

        // set dialog message
        alertDialogBuilder.setCancelable(false).setPositiveButton("OK",
                new DialogInterface.OnClickListener()
                {
            public void onClick(DialogInterface dialog,int which)
            {
                 String value = userInput.getText().toString();
                 currentIntervalChoice=Integer.parseInt(value);
                toggleLogging(AppSettings.getServiceRunning(MainActivity.this),
                        AppSettings.setLoggingInterval(MainActivity.this,currentIntervalChoice));
                dialog.dismiss();
                // return;  
            }
              });
        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();
        // show it
        alertDialog.show();
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("isFirstRun", false);
        editor.commit();
    }
    else
    {

        Toast.makeText(getApplicationContext(),"in 1st run false", Toast.LENGTH_LONG).show();       
        toggleLogging(AppSettings.getServiceRunning(MainActivity.this),
                AppSettings.setLoggingInterval(MainActivity.this,currentIntervalChoice));
    }

Upvotes: 1

Views: 256

Answers (1)

Aleks G
Aleks G

Reputation: 57316

From what I see, you're using shared preferences to store/check whether this is the first run of the app. Your else part will not be executed on the same run (because it's either first run or not). What you should do instead is store the value entered by the user into the shared preferences, exactly the same way as you're storing the isFirstRun flag. Then in your else part, simply read that value from your shared preferences. Something like this:

final SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
if (settings.getBoolean("isFirstRun", true))
{
    ...

    alertDialogBuilder.setCancelable(false).setPositiveButton("OK",
            new DialogInterface.OnClickListener()
            {
        public void onClick(DialogInterface dialog,int which)
        {
             String value = userInput.getText().toString();
             int currentIntervalChoice=Integer.parseInt(value);
             Editor edit = settings.edit();
             editor.putBoolean("isFirstRun", false);
             editor.putInt("currentIntervalChoice", currentIntervalChoice);
             editor.commit();

             ...
        }
          });
    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();
    // show it
    alertDialog.show();
}
else
{
    int currentIntervalChoice = settings.getInt("currentIntervalChoice", 0);

    ...
}

Note that I removed unrelated code - you'll probably need to keep it. I also moved storing your isFirstRun flag inside the onClick. You may also want to add some validation on this value, but it's down to your logic.

Upvotes: 2

Related Questions