5120bee 5120bee - 3 years ago 194
Android Question

String array gets every character input instead of full word?

I have a logical error in my code below that produces the following result when an array-list

personArray
is printed out in my
Activity_2
:

Scenario:

If the user types their name as "John" in the
EditText
field
name
it will get added to the
personArray
array-list. Then it will get passed into my
Activity_2
where it is printed out.

However, upon doing so the print out is this:

[J, Jo, Joh, John]

It's the same for my other
EditText
fields. I just want to add the whole word to my
personArray
and not every character as I typed it in.

My variables:

ArrayList<String> personArray = new ArrayList<>;
EditText name = (EditText) findViewById(R.id.name);


My
Text Changed Listener
inside my
onCreate()
in
Activity_1
:

name.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int i, int i1, int i2){
//it's supposed to add the user's FULL name input to personArray
//however it ends up adding EVERY character input instead
personArray.add(name.getText().toString().trim());
}
});

// repeat code for input for address, country, etc.


And my
Intent
inside my button click to go to my next
Activity_2
:

Intent intent = new Intent(Activity_1.this, Activity_2.class);
intent.putStringArrayListExtra("person", personArray);
startActvity(intent);


And getting the personArray string array in
Activity_2
:

final List<String> personList = getIntent().getStringArrayListExtra("person");
TextView txtPerson = (TextView) findViewById(R.id.txtPerson);
txtPerson.setText(personList);


I'm not sure where to place my
personArray.add(name.getText().toString().trim());

Answer Source

Try to add the code in

@Override public void afterTextChanged(Editable s) {
 // do stuff 
}

UPDATE Or you can get the whole input by using focus.

myEditText.setOnFocusChangeListener(new OnFocusChangeListener() { 
@Override public void onFocusChange(View v, boolean hasFocus) { 
            if (!hasFocus) {
             String editTextInput = myEditText.getText().toString();
             }

     } 
    });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download