3

I tried to make an app about user inputs the last name, first name and address. What i was trying to do was to let the user input in an EditText and display it on a TextView but i cannnot display the following text because there is an error on runtime. What it does is if i click the submit button it will display the user input but what it says on the TextView is "android.support.v7.widget.TextView....etc". I just started Android programming now and everything is confusing. Please tell me the most basic stuff to solve this problem.

 public class MainActivity extends AppCompatActivity {

            EditText fname, lname;
            TextView display;
            Button result;

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


                fname = (EditText) findViewById(R.id.etFirstname);
                lname = (EditText) findViewById(R.id.etLastname);
                display = (TextView) findViewById(R.id.tvResult);
                result = (Button) findViewById(R.id.btnSubmit);



                result.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        display.setText("Name: " + lname + ", " + fname);
                    }
                });
        }

    }
USER9561
  • 1,084
  • 3
  • 15
  • 41
Kyu Jo Rim
  • 41
  • 1
  • 4

6 Answers6

7

You have defined correct the variables, but you have to get the value of what the edit text has. so replace your code with this.

 result.setOnClickListener(new View.OnClickListener()
 {
    @Override
    public void onClick(View v)
    {
         display.setText("Name: " + lname.getText().toString() + ", " + fname.getText().toString());
    }
  });
Antonios Tsimourtos
  • 1,676
  • 1
  • 14
  • 37
3

You need to fetch the text from the EditText widgets when you want to show it in the TextView. Right now you just print the object into the setText.

Try this instead.

display.setText("Name: " + lname.getText().toString() + ", " + fname.getText().toString());
malmling
  • 2,398
  • 4
  • 19
  • 33
3

Your are not fetching the text of EditText. Fetch the text of the EditText using lname.getText().toString() and then set this text to your TextView

Try this code:

result.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
         display.setText("Name: " + lname.getText().toString() + ", " + fname.getText().toString());
    }
});
USER9561
  • 1,084
  • 3
  • 15
  • 41
1

lname and fname are EditText you cant concatenate 2 EditText with string.

You need to do

display.setText("Name: " + lname.getText() + ", " + fname.getText());
Hugo Houyez
  • 470
  • 3
  • 19
  • If you call the function toString(), then you dont concatenate 2 EditText you concatenate 2 String. String toString() it returns String, so as i said before, you CANT concatenate 2 EditText with String. – Hugo Houyez Sep 20 '16 at 08:08
  • I suggest you give it a try and see what happens – Tim Sep 20 '16 at 08:16
  • It's the reason why the topic was created, he tried to concatenate string with 2 edittext and he had errors. – Hugo Houyez Sep 20 '16 at 08:18
1

My suggestion is to modularize your code.

First create a bean class which contains fName and lName as two string field and overides toString() method as below.

class Person {
    private String fName;
    private String lName;

    // Getter and Setter methods, which you can write.

    public String toString() {
        return "Name: " + lname + ", " + fname;
    }
}

Create bean object and put listener on your both edittext in activty.

Person person = new Person();

fname.addTextChangedListener(new TextWatcher() {

          public void afterTextChanged(Editable s) {

            //set bean object field fName here. 
            person.setFname(s.toString());
          }

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          public void onTextChanged(CharSequence s, int start, int before, int count) {}
       });

same for other edittext field.

lname.addTextChangedListener(new TextWatcher() {

              public void afterTextChanged(Editable s) {

                //set bean object field lName here. 
                person.setLName(s.toString());
              }

              public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

              public void onTextChanged(CharSequence s, int start, int before, int count) {}
           });

Then inside onclick listener simply return person object toString() method.

result.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        display.setText(person.toString());
                    }
                });
Sachin Saxena
  • 604
  • 5
  • 10
0

to build upon Hugo Houyez answer

display.setText(String.format("Name: %1$s, %2$s",lname.getText().toString(),fname.getText().toString);

now you can see the full string you want to show as first value.

per %s value you can add a string value. the %1$s value means that the first string will be showed here %2$s will show the second string and so forth.

quantum apps
  • 938
  • 2
  • 13
  • 25