Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText Values in Android Fragment do not refresh

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?

like image 911
Lukas H Avatar asked Oct 20 '12 09:10

Lukas H


2 Answers

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

or try

Intent.FLAG_ACTIVITY_CLEAR_TOP on tab change.

like image 55
J.D. Avatar answered Nov 04 '22 12:11

J.D.


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();
}
like image 30
samus Avatar answered Nov 04 '22 13:11

samus