Question

I'm having difficulties getting a string containing the user's input and displaying it in a textview when the button is clicked. For example, if the user types 10 and click the button, I want the textview to display 10. I get a warning saying that answerINT is never used.

The code I'm having difficulties with

                //Fetches user answer and converts it into an integer
                EditText answer = (EditText)findViewById(R.id.editanswer); {
                if (answer.getText().toString().length() > 0){
                    int answerInt = Integer.parseInt(answer.getText().toString());

                    //Display answer in textview on click
                    TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer);
                    displayanswer.setText(answer);

                }

The coode in it's context

            findViewById(R.id.btnok).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Log.d("onClick", "OK button was clicked");

                //Fetches user answer and converts it into an integer
                EditText answer = (EditText)findViewById(R.id.editanswer); {
                if (answer.getText().toString().length() > 0){
                    int answerInt = Integer.parseInt(answer.getText().toString());

                    //Display answer in textview on click
                    TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer);
                    displayanswer.setText(answer);

                }
            //Displaying a toast if no answer is provided
                else
                {
                    Toast.makeText(getApplicationContext(), "No answer provided!",
                           Toast.LENGTH_SHORT).show();
                }


                    }
Was it helpful?

Solution

Try this

 displayanswer.setText(" "+answerInt);

OR

displayanswer.setText(String.valueOf(answerInt));

OTHER TIPS

Try this:

displayanswer.setText(""+answerInt);

You can use this:

String.valueOf(yourint);

You can simply do it this way:

if (answer.getText().toString().length() > 0){
                //Display answer in textview on click
                TextView displayanswer = (TextView) findViewById(R.id.tvdisplayanswer);
                displayanswer.setText(answer.getText().toString());

            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top