11

I'm using the Viewpager to switch between 3 fragments, everything is working fine, except the refreshing of the second tab (or fragment). In this tab, I have a picture, some static Textviews, some dynamic TextViews and some EditText fields.

Everytime the second tab is selected, there will be called setText() on all dynamic fields. TextView components and the spinner are refreshing and updating their contents, but EditText elements do not. I don’t understand why these fields are not updating. After tab change, I call notifiyDataSetChanged() in TabsAdapter. It calls onViewCreated() everytime I change the tab. In onViewCreated() of the second fragment I am changing the contents of the elements.

That’s the code of my fragment:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    hA = (HelloAndroidActivity)this.getSherlockActivity();
    appState = ((TestApplication)this.getSherlockActivity().getApplicationContext());
    final PersistenceHelper pH = new PersistenceHelper(appState);
    m = pH.readMitarbeiter(toDisplay);

     // Inflate the layout for this fragment
    v = inflater.inflate(R.layout.detailfragment, container, false);

    if(m==null) {           
        //If Mitarbeiter is empty
        pH.closeDB();
        return v;
    }

    //Inflating Elements

    employeeName = (TextView)v.findViewById(R.id.employee_name);
    cDate = (TextView)v.findViewById(R.id.department);
    currentPlace = (TextView)v.findViewById(R.id.place_label);

    comment_text = (EditText)v.findViewById(R.id.comment_text);
    reachable = (EditText)v.findViewById(R.id.reachable_text);
    state = (Spinner)v.findViewById(R.id.spinner_state);
    durchwahl = (EditText)v.findViewById(R.id.durchwahl);
    department = (EditText)v.findViewById(R.id.department_edit);
    email = (EditText)v.findViewById(R.id.email_edit);

    img = (ImageView)v.findViewById(R.id.userPic);

    changeData = (Button)v.findViewById(R.id.changeButton);

    //Setting Elements

    employeeName.setText(m.getL_name());
    currentPlace.setText(m.getStatus());    



    comment_text.setText(m.getBemerkung());


    reachable.setText(m.getErreichbar());       

    email.setText(m.getS_name()+"");        
    durchwahl.setText(m.getDurchwahl()+"",TextView.BufferType.EDITABLE);


    department.setText(m.getFunktion(),TextView.BufferType.NORMAL);

    //Spinner

    String[] values = { "Anwesend", "Mittagspause" , "Ausser Haus" , "Dienstreise", "Krankenstand" ,"Zeitausgleich", "Urlaub","Abwesend" };
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this.getSherlockActivity(), android.R.layout.simple_spinner_item,values);
    state.setAdapter(spinnerArrayAdapter);

    state.setSelection(StateMapper.returnState(m.getStatus()));

    //Image
    //.......
    return v;
}

As I mentioned before, my Image, TextView and Spinner Elements are refreshing their content. I also checked the content of all variables, everything seems to be fine, except these EditText elements. If I cast the EditText elements into TextView, the content is changing (in code but not in the GUI). What also makes me desperate is, that the EditText refreshes the first time I set the value.

Has anybody an idea, how I’m able to refresh the content of my EditText fields?

Lukas H
  • 123
  • 1
  • 6

3 Answers3

15

i am not sure but try onResume() and set your text in resume state.

or try

Intent.FLAG_ACTIVITY_CLEAR_TOP on tab change.

J.D.
  • 1,401
  • 1
  • 12
  • 21
  • 1
    THX Setting the values in onResume() solved the problem.... However I don't understand why I can't change the EditText values in onCreateView() – Lukas H Oct 22 '12 at 06:20
  • If an event handler is assigned to the TextView/EditText in question, it should also be done in onResume(), or else the caching/updating issue will occur as well. – samus Mar 12 '13 at 22:04
  • I had previously believed that recreate() will completely re run an activity as a new instance, which would mean all views are refreshed and reloaded with any new data.However, sadly after reconstructing my code several times I happened across the official docs and then this post. Refreshing a view's contents are more reliably done in onResume(), as suggested here. – Xijukx Aug 25 '19 at 03:21
1

You could also try posting a runnable to the message queue so that the EditText's are updated after rendering (in MonoDroid/C#, see How to run a Runnable thread in Android? for java):

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
    EditText et = FindViewById<EditText>(...);

    et.Post(() => { et.Text = "content"; });
}

Also, if you have a TextChanged event handler (say for displaying a save icon/button when the text is changed), post it in the runnable as well, and do it after the et.Text assignment. Otherwise, the TextChanged event will fire when the initial et.Text content is assigned, causing the TextChanged event to fire (ie, and the save button showing) when the USER hasn't changed anything:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
{
    EditText et = FindViewById<EditText>(...);

    et.Post(() => 
        { 
            et.Text = "content"; 
            et.TextChanged += TextChangedHandler;
        });
}

private void TextChangedHandler(object o, EventArgs args)
{
    ShowSaveButton();
}
Community
  • 1
  • 1
samus
  • 6,102
  • 6
  • 31
  • 69
  • Make sure to "unregister" `TextChanged` event handlers in `OnDestroy`, otherwise they could inadvertently fire (say when reloading view, especially if the `TextView` is within a `TabHost`/`TabWidget`). – samus Oct 05 '17 at 18:04
0

I came across this issue in 2021. I just had to call findViewById again right before setting the text.

    myEditText = (EditText)view.findViewById(R.id.myEditText);
    myEditText.setText(String.valueOf(newValue));
ZP007
  • 582
  • 7
  • 12