Change TextView if Spinner or EditText is changed
I am a beginner android developer. I have TextView
, Spinner
and EditText
. TextView
will change to match Spinner
or EditText
and I know how to do it. My problem is how to get both Spinner
and EditText
work together, so when I select an item from Spinner
, it checks what I have in EditText
before printing it in TextView
, and it should be the same for EditText
.
EditText
...
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!mEditText.getText().toString().trim().isEmpty()) {
value = Double.parseDouble(mEditText.getText().toString());
mTextView.setText(value + "");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Spinner code:
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (mMapUnit.containsKey(lengthKey[position])) {
if (!mEditText.getText().toString().trim().isEmpty()) {
value = Double.parseDouble(mEditText.getText().toString());
// calculate
mTextView.setText(result + "");
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
source to share
This method can be used to place editext text and selected item in TextView
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!mEditText.getText().toString().trim().isEmpty()) {
value = Double.parseDouble(mEditText.getText().toString());
mTextView.setText(value + "" + mSpinner.getAdapter().getItem(mSpinner.getSelectedItemPosition()).toString());
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (mMapUnit.containsKey(lengthKey[position])) {
if (!mEditText.getText().toString().trim().isEmpty()) {
value = Double.parseDouble(mEditText.getText().toString());
// calculate
mTextView.setText(value + "" + mSpinner.getAdapter().getItem(position).toString());
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
A little explanation of the code above:
I assume you know what an Adapter is, but I'm going to explain it anyway; An adapter is an object that contains information that should be displayed in a view (in this case, a Spinner).
This adapter is connected to the Spinner view and calls the mSpinner .getAdapter () method
You will receive the same adapter instance that is attached to the Spinner. Since the Spinner works in conjunction with the adapter, the position of the element in the Spinner will be in the same position in the adapter.
So, you can call mSpinner.getAdapter () .getItem (position); to get the element at position.
Then you should see what the adapter is using for the ListTypes. If this is a line you can do
String itemText = ((String) mSpinner.getAdapter (). GetItem (position) ) ;
When the onTextChanged callback is called, the EditText text changes (or changes) and it doesn't think about the Spinner. This way you don't get Spinner parameters (like current position or id). Thus, you will need to get it manually with the explanation above.
When the OnItemSelected callback is called, the Spinner has changed and, you guessed it, this time we have those parameters that I mentioned. This way you don't have to get the selected item (which is dangerous because the Spinner can still handle the change and the selected item of the spinner can be different from the position provided by the callback)
So you have to use the callback position parameter.
Since I couldn't find the "result" variable, I assumed that you pointed to the "value" variable in the OnItemSelectedListener.
Good luck!
source to share
you can just use boolean to test conditions for EditText or Spinner. create boolean with default false for Spinner and EditText. in the afterTextChanged method of the TextWatcher method and in the Spinner set its boolean value to true and the other way (Spinner and EditText), check this.
perhaps this help.
source to share