EditText values ​​in Android Fragment not updated

I use Viewpager to switch between 3 fragments , everything works fine, except for updating the second tab (or fragment ). On this tab, I have an image, some static Textviews , some dynamic Textviews and some EditText .

Each time the second tab is selected, setText() will be called in all dynamic fields. The TextView and spinner components update and update their contents, but the EditText elements do not. I do not understand why these fields are not updated. After changing the tab, I call notifiyDataSetChanged() on the TabsAdapter . It calls onViewCreated() every time I change the tab. In onViewCreated() second fragment, I change the contents of the elements.

Here is the code for my snippet:

  @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 earlier, my images, TextView, and Spinner Elements update their content. I also checked the contents of all the variables, everything seems fine except for these EditText elements. If I insert EditText elements into a TextView, the content changes (in the code, but not in the GUI). What also makes me desperate is that EditText updates the first time I set the value.

Does anyone have an idea how I can update the contents of my EditText fields?

+6
source share
2 answers

I'm not sure, but I will try onResume() and set the text to resume.

or try

Intent.FLAG_ACTIVITY_CLEAR_TOP when changing tabs.

+9
source

You can also try to publish runnable to the message queue so that EditText is updated after rendering (in MonoDroid / C #, see How to run 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"; }); } 

In addition, if you have a TextChanged event handler (say, to display the save icon / button when changing text), send it to runnable and do it after et.Text is assigned. Otherwise, the TextChanged event will fire when the original content of et.Text is assigned, as a result of which the TextChanged event will fire (i.e., the save button) when the USER has not 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(); } 
+1
source

Source: https://habr.com/ru/post/928172/


All Articles