Android EditText setText does not update text

Hey. I do not know what's happening. I am trying to change the text of an EditText while creating a DialogFragment , but the EditText does not update the text. If I call getText().length() , I notice that the contents of the EditText changed. But the visual retains the same, just empty.

Why?

Thanks in advance to people

Here is the code:

 public class ItemNameDialog extends DialogFragment { @Override public Dialog onCreateDialog(final Bundle bundle) { System.out.println("ON CREATE DIALOG WAS CALLED"); //This appears on LogCat, so this method is called.. the problem is not that AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("Configure an item to count:"); LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.itempick_layout, null); builder.setView(view); final CheckBox box = (CheckBox) view.findViewById(R.id.itemSenseCheckBox); final EditText itemNameBox = (EditText) view.findViewById(R.id.itemNameText); final Spinner spinner = (Spinner) view.findViewById(R.id.itemsDefault); final int viewIDClicked = getArguments().getInt(clickableViewID); final String actualName = getArguments().getString(actualNameItemView); System.out.println("H - before: " + itemNameBox.getText().toString().length()); //it appears on logcat "H - before: 0" itemNameBox.setText(actualName); System.out.println("H - after: " + itemNameBox.getText().toString().length()); //it appears on logcat "H - before: 3" so why not changing ? return builder.create(); } } 
+7
source share
1 answer

My problem was that below this code and until the onCreateDialog method completed, I had several methods controlling the spinner.
The first element of this counter is to β€œselect nothing,” and in this selection of the element I simply deleted the contents of this edit text, for example:

 @Override public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { if(pos == 0){ if(editText.length() != 0 ) editText.setText(""); } 

And I did not know that when creating the counter, it raises the "onItemSelected" event, and because of this, the edittext was erased every time, even when I did not click on this spinner element.

> Therefore, I managed to overcome this with a simple Boolean. Every time the onCreateDialog method puts a boolean value in true, and then my onItemSelected just works when this bolean is false.
Like the code below:

 @Override public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { if(firstGoingSpinner){ firstGoingSpinner = false; }else{ if(pos == 0){ if(editText.length() != 0 ) editText.setText(""); }else{ editText.setText(""+parent.getItemAtPosition(pos)); Editable etext = editText.getText(); Selection.setSelection(etext, editText.length()); } } } 


I hope this helps someone in the future;)

+4
source

All Articles