Save user input string to variable on button click

KP123

in my application the user will input a channel they want to subscribe to in an editable text field. Upon clicking the subscribe button, the variable that is placed in my method to subscribe the user should be updated to that of the users choice and then a toast will appear notifying the user that they are subscribed to this channel.

At the bottom, I get the string value that the user inputs in the EditText field, but I don't think I'm putting it in the right place. Can someone take a look at it and let me know what I'm doing wrong? Let me know if I wasn't clear or am missing something and I can try to explain better. Thanks!

public class MainActivity extends ActionBarActivity {

private Button channelSubscribeButton;
private EditText subscribeChannelEditText;

//Declare variable
String subscribeChannel = null;


//-------------------------Access PubNub API-------------------------//
Pubnub pubnub = new Pubnub("pub-c-940c4776-36ff-425f-9677-f1c904a9d57b", "sub-c-cf42b292-a8bf-11e4-85d5-0619f8945a4f");


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

    channelSubscribeButton = (Button) findViewById(R.id.subscribe_button);
    subscribeChannelEditText = (EditText) findViewById(R.id.channel_name);



    //-------------------------Subscribe to a Channel to Receive Messages-------------------------//



    try {
//THIS IS WHERE I AM INPUTTING THE VARIABLE THAT CONTAINS THE USER INPUTTED TEXT//
//I AM GETTING ERROR THAT SAYS IT CAN'T RESOLVE THE SYMBOL 'subscribeChannel'//

        pubnub.subscribe(subscribeChannel, new Callback() {

                    @Override
                    public void connectCallback(String channel, Object message) {
                        Log.d("PUBNUB", "SUBSCRIBE : CONNECT on channel:" + channel
                                + " : " + message.getClass() + " : "
                                + message.toString());
                    }

                    @Override
                    public void disconnectCallback(String channel, Object message) {
                        Log.d("PUBNUB", "SUBSCRIBE : DISCONNECT on channel:" + channel
                                + " : " + message.getClass() + " : "
                                + message.toString());
                    }

                    public void reconnectCallback(String channel, Object message) {
                        Log.d("PUBNUB", "SUBSCRIBE : RECONNECT on channel:" + channel
                                + " : " + message.getClass() + " : "
                                + message.toString());
                    }

                    @Override
                    public void successCallback(String channel, Object message) {
                        Log.d("PUBNUB", "SUBSCRIBE : " + channel + " : "
                                + message.getClass() + " : " + message.toString());
                    }

                    @Override
                    public void errorCallback(String channel, PubnubError error) {
                        Log.d("PUBNUB", "SUBSCRIBE : ERROR on channel " + channel
                                + " : " + error.toString());
                    }
                }
        );
    } catch (PubnubException e) {
        Log.d("PUBNUB", e.toString());
    }

public void onChannelButtonClick(View view) {

    subscribeChannel = String.valueOf(subscribeChannelEditText.getText());

    String yourSubscribeChannel = "Subscribed to the " + subscribeChannel + " Channel";

    Toast.makeText(this, yourSubscribeChannel, Toast.LENGTH_LONG).show();

    return subscribeChannel;
}
Manuel Ramírez

Your subscribeChannel variable is only visible within the scope of your onChannelButtonClick method. Declare it outside along with your instance variables.

public void onChannelButtonClick(View view) {    
    //here you set it and then it's lost.
    String subscribeChannel = String.valueOf(subscribeChannelEditText.getText());    
    String yourSubscribeChannel = "Subscribed to the " + subscribeChannel + " Channel";    
    Toast.makeText(this, yourSubscribeChannel, Toast.LENGTH_LONG).show();    
}

Instead do:

//declare it.
String subscribeChannel = null;

And then on your method assign the value.

public void onChannelButtonClick(View view) {    
    //assign it.
    subscribeChannel = String.valueOf(subscribeChannelEditText.getText());    
    String yourSubscribeChannel = "Subscribed to the " + subscribeChannel + " Channel";    
    Toast.makeText(this, yourSubscribeChannel, Toast.LENGTH_LONG).show();    
}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to input value from user and save in variable wxDev-C++

분류에서Dev

User input string into string array

분류에서Dev

Save and retrieve user input from database with javascript?

분류에서Dev

downloading a picture or showing save popup for a picture on click on a button

분류에서Dev

How to save entries into db on submit button click only

분류에서Dev

How to verify the user input in a textfield to compare it with a variable?

분류에서Dev

batch variable from user input treated differently

분류에서Dev

read input text value when button click in table

분류에서Dev

generate alert on button click based on input type hidden

분류에서Dev

How to update PHP form when user click submit button?

분류에서Dev

How to convert user-input into string

분류에서Dev

How to convert user input string to pig latin?

분류에서Dev

How to iterate user list, change a variable, and save to database?

분류에서Dev

Design: Displaying different button for different variable (ie. user,status)

분류에서Dev

Accessing a local variable whose name comes from user input

분류에서Dev

Writing a query that contains variable WHERE based on user input

분류에서Dev

How use Jquery Save Cookie inthe browser when click button window reload

분류에서Dev

Save Calculated Value from Spawned Child Form to Parent After Button Click

분류에서Dev

Issues with structure arrays and obtaining array string from user integer input

분류에서Dev

How to store user string input in array, C++

분류에서Dev

How do i pass input variable to a C++ class method invoked in many places based on user input?

분류에서Dev

How to save user input data from multiple forms (in a single template/web page) to the sqlite database in django?

분류에서Dev

What data structures for python are used to save user input data( usernames, their passwords, etc.)

분류에서Dev

After a succesful sign-in when user click on the back button of the browser, "login" page should not appear

분류에서Dev

How should I save string from the Radio Button in arraylist and display it on next page in text box

분류에서Dev

Ajax multiple button click

분류에서Dev

Display Checkboxes on Click of a button

분류에서Dev

Android NullPointerException on button click

분류에서Dev

click button at end of function

Related 관련 기사

  1. 1

    How to input value from user and save in variable wxDev-C++

  2. 2

    User input string into string array

  3. 3

    Save and retrieve user input from database with javascript?

  4. 4

    downloading a picture or showing save popup for a picture on click on a button

  5. 5

    How to save entries into db on submit button click only

  6. 6

    How to verify the user input in a textfield to compare it with a variable?

  7. 7

    batch variable from user input treated differently

  8. 8

    read input text value when button click in table

  9. 9

    generate alert on button click based on input type hidden

  10. 10

    How to update PHP form when user click submit button?

  11. 11

    How to convert user-input into string

  12. 12

    How to convert user input string to pig latin?

  13. 13

    How to iterate user list, change a variable, and save to database?

  14. 14

    Design: Displaying different button for different variable (ie. user,status)

  15. 15

    Accessing a local variable whose name comes from user input

  16. 16

    Writing a query that contains variable WHERE based on user input

  17. 17

    How use Jquery Save Cookie inthe browser when click button window reload

  18. 18

    Save Calculated Value from Spawned Child Form to Parent After Button Click

  19. 19

    Issues with structure arrays and obtaining array string from user integer input

  20. 20

    How to store user string input in array, C++

  21. 21

    How do i pass input variable to a C++ class method invoked in many places based on user input?

  22. 22

    How to save user input data from multiple forms (in a single template/web page) to the sqlite database in django?

  23. 23

    What data structures for python are used to save user input data( usernames, their passwords, etc.)

  24. 24

    After a succesful sign-in when user click on the back button of the browser, "login" page should not appear

  25. 25

    How should I save string from the Radio Button in arraylist and display it on next page in text box

  26. 26

    Ajax multiple button click

  27. 27

    Display Checkboxes on Click of a button

  28. 28

    Android NullPointerException on button click

  29. 29

    click button at end of function

뜨겁다태그

보관