Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText setText not updating the text

Hi there. I dont know what is happening. I am trying to change the text of an EditText when creating a DialogFragment, but the EditText is not updating the text. If I call getText().length() I notice that the content of the EditText changed. But, the visual keeps the same, just empty.

Why?

Thanks in advance 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();
    }
}
like image 568
TiagoM Avatar asked Mar 30 '13 19:03

TiagoM


1 Answers

My problem was that below that code, and before the method onCreateDialog ends, I had a few methods controlling a spinner.
The first item of that spinner has the purpose of "select nothing" and in that item choice I was just erasing the content of that edit text, like this:

@Override
public void onItemSelected(AdapterView<?> parent, View view,
                int pos, long id) {

if(pos == 0){
    if(editText.length() != 0 )
        editText.setText("");
}

And I didnt know that while creating the spinner, it does trigger the event "onItemSelected" and because of that, the edittext was everytime erased, even when I didnt click on that item of the spinner..

So I managed to overcome this by a simple boolean. Every time the method onCreateDialog I put the boolean to true, and then my onItemSelected just operate when that 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 ;)

like image 118
TiagoM Avatar answered Sep 27 '22 18:09

TiagoM