7

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();
    }
}
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
TiagoM
  • 3,458
  • 4
  • 42
  • 83
  • 2
    try changing the color of the font in the edittext – karan Mar 30 '13 at 20:02
  • didnt help mate.. but thanks for your effort – TiagoM Mar 30 '13 at 20:04
  • 1
    @DarkLink : instead of printing length try to print `itemNameBox.getText().toString()` and check getting text in log or not – ρяσѕρєя K Mar 30 '13 at 20:06
  • Hey guys sorry it was my fault, I had some more code updating a spinner content, and this one has deleting the content of that edittext.. It was my bad, didnt know this peace of code were important. Dont know what to do to this question.. delete? – TiagoM Mar 30 '13 at 20:08
  • 3
    instead post your solution..it will be helpful to the user who does similar mistake accidently – karan Mar 30 '13 at 20:10
  • 1
    post your mistake as an answer and accept it as it might help others that have done the same mistake. – Parvaz Bhaskar Mar 30 '13 at 20:11

1 Answers1

4

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 ;)

TiagoM
  • 3,458
  • 4
  • 42
  • 83