3 Replies - 185 Views - Last Post: 06 June 2014 - 01:58 PM

#1 zaind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-December 12

Problem with value of variable in Java, Android Studio.

Posted 04 June 2014 - 04:49 PM

I have an integer variable called 'selectedname' which is declared as shown below.

public class MainActivity extends Activity implements View.onclickListener, AdapterView.OnItemClickListener  {

   int selectedname;




So when the app starts an Alert Dialog with two buttons asks the user if they want to tell the first or the last name.
The two buttons are 'first' and 'last'.
The problem I'm facing is that in the code the value of selectedname is suppose to change depending on the users choice as shown below.

    public void displayWelcome() {

            //*other code comes here but it works fine*

            AlertDialog.Builder choice = new AlertDialog.Builder(this);
            choice.setTitle("Yo!");
            choice.setMessage("First name or last");

            choice.setPositiveButton("First", new DialogInterface.onclickListener() {

                public void onclick(DialogInterface dialog, int whichButton) {
                     selectedname = 1;
                }
            });

            choice.setNegativeButton("Last", new DialogInterface.onclickListener() {
                public void onclick(DialogInterface dialog, int whichButton) {
                    // get the button pressed
                    selectedname = 2;
            });

            choice.show();
        }




but in actual the value of selectedname remains zero and therefore the following if statement fails.

if (selectedname == 1 ){

            namedialog.setMessage("So, whats the first name");

            final EditText input = new EditText(this);
            namedialog.setView(input);

            namedialog.setPositiveButton("OK", new DialogInterface.onclickListener() {

                public void onclick(DialogInterface dialog, int whichButton) {

                    String inputFirstName = input.getText().toString();

                    SharedPreferences.Editor e = mSharedPreferences.edit();
                    e.putString(PREF_FIRSTNAME, inputFirstName);
                    e.commit();

                    Toast.makeText(getApplicationContext(), "Welcome, " + inputFirstName + "!", Toast.LENGTH_LONG).show();
                }
            });
            namedialog.show();
        }

        else if (selectedname == 2 ){

            namedialog.setMessage("So, whats the last name");

            final EditText input = new EditText(this);
            namedialog.setView(input);

            namedialog.setPositiveButton("OK", new DialogInterface.onclickListener() {

                public void onclick(DialogInterface dialog, int whichButton) {

                    String inputLastName = input.getText().toString();

                    SharedPreferences.Editor e = mSharedPreferences.edit();
                    e.putString(PREF_LASTNAME, inputLastName);
                    e.commit();

                    Toast.makeText(getApplicationContext(), "Welcome, " + inputLastName + "!", Toast.LENGTH_LONG).show();
                }
            });
            namedialog.show();
        }
    }



I know its a stupid question but can you please help me out here?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with value of variable in Java, Android Studio.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4316
  • View blog
  • Posts: 12,096
  • Joined: 18-April 07

Re: Problem with value of variable in Java, Android Studio.

Posted 05 June 2014 - 12:06 PM

First of all you really should set your variable with a default value. This will make sure that is always a valid value. This will also show you, when you get down to your if statements, if the value has been changed to something other than 1 or 2 (aka corrupted).

Now is it possible that the second code is running before the user has a chance to click? I guess what I am asking is where does the second piece of code you have there run in relation to the first piece? I assume you are calling displayWelcome() and then directly going into the second piece after the call returns? Just out of curiosity have you tried using the 'this' keyword when setting it? I don't think you need to have it, but it would be an interesting test.

:)

This post has been edited by Martyr2: 05 June 2014 - 12:06 PM

Was This Post Helpful? 1
  • +
  • -

#3 zaind  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 10-December 12

Re: Problem with value of variable in Java, Android Studio.

Posted 05 June 2014 - 12:53 PM

View PostMartyr2, on 05 June 2014 - 12:06 PM, said:

First of all you really should set your variable with a default value. This will make sure that is always a valid value. This will also show you, when you get down to your if statements, if the value has been changed to something other than 1 or 2 (aka corrupted).

Now is it possible that the second code is running before the user has a chance to click? I guess what I am asking is where does the second piece of code you have there run in relation to the first piece? I assume you are calling displayWelcome() and then directly going into the second piece after the call returns? Just out of curiosity have you tried using the 'this' keyword when setting it? I don't think you need to have it, but it would be an interesting test.

:)/>


Thank you. Anyways I found the problem. It was actually an AlertBox which moved back to a previous method after alertbox.show() and i was adding the if statement after alertbox.show().
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Problem with value of variable in Java, Android Studio.

Posted 06 June 2014 - 01:58 PM

View PostMartyr2, on 05 June 2014 - 07:06 PM, said:

First of all you really should set your variable with a default value. This will make sure that is always a valid value. This will also show you, when you get down to your if statements, if the value has been changed to something other than 1 or 2 (aka corrupted).


It's an instance variable. It already has a default value.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1