Reputation: 4659
I know it's so stupid to ask this question but I can't even find the error for the following to solve, I did everything as it should be still it won't store string value in the String name
I am implementing EditText
in one of my fragments
and storing the value in an String
but the values won't store in the String
. As per I know I have done everything correctly but this thing won't work at all.
In the onCreateView
I am performing this code:
EditText av i= (EditText) v.findViewById(R.id.nameAndroid);
String name = avi.getText().toString();
And I am performing an operation where I check if the user has entered anything string based or not, then I will have it executed another part of code to do something with the entered String
with this code below:
if(name.matches("")){
Log.i(TAG, "input is empty");
} else{
//code
}
But everytime I am getting the log that the input is empty and the string is not storing the value... I am not able to find the error here.. what am I doing wrong?
Upvotes: 0
Views: 708
Reputation: 2136
It's too early to save the value of EditText in onCreateView - it's just created and user even didn't see it yet.
You have 2 ways:
a. If you have a button (user enters data and clicks a button), then add something like this in your onClick
method:
Editable e = yourEditText.getText();
String string = "";
if (e != null) string = e.toString();
b. If you do not have a button, you can add a TextWatcher:
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
Editable e = yourEditText.getText();
String string = "";
if (e != null) string = e.toString();
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});
Note that you should save the value after user entered it - and onCreateView user even didn't see the view yet. In a case the user clicks the button - and though verifies that he entered something in the field. In b case we watch the changes in the field so the value is saved only if there is something to save.
You can save the value when user goes back/changes the screen, e.g. in onPause.
Upvotes: 2
Reputation: 2911
Here you are saving the empty EditText just after retrieve it. You have to add a listener on the EditText ot see when it is edited.
Use a TextWatcher : http://developer.android.com/reference/android/text/TextWatcher.html
avi.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
name= avi.getText().toString();
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence s, int a, int b, int c) {
}
});
Upvotes: 2
Reputation: 8774
getText()
of EditText
gives you the content of the EditText
at the time it is called, so you have to call name= avi.getText().toString();
when this "operation" happens, not in onCreateView
or it will only store the string value of the EditText
at that time.
Upvotes: 1