Android Fragment中的EditText值不会刷新 [英] EditText Values in Android Fragment do not refresh
问题描述
我正在使用Viewpager
在3个fragments
之间切换,除了刷新第二个选项卡(或fragment
)之外,其他所有功能都工作正常.在此选项卡中,我有一张图片,一些静态Textviews
,一些动态TextViews
和一些EditText
字段.
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.
每次选择第二个选项卡时,所有动态字段上都会调用setText()
. TextView组件和微调框正在刷新和更新其内容,但EditText元素却没有.
我不明白为什么这些字段没有更新.更改制表符后,我在TabsAdapter
中调用notifiyDataSetChanged()
.每当我更改选项卡时,它就会调用onViewCreated()
.在第二个片段的onViewCreated()
中,我正在更改元素的内容.
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.
这是我片段的代码:
@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;
}
如前所述,我的Image,TextView和Spinner Elements正在刷新其内容.我还检查了所有变量的内容,除了这些EditText元素之外,其他一切似乎都还不错.如果我将EditText元素转换为TextView,则内容正在更改(在代码中,但在GUI中未更改).令我感到绝望的是,第一次设置该值时,EditText会刷新.
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.
有人知道我如何刷新EditText
字段的内容吗?
Has anybody an idea, how I’m able to refresh the content of my EditText
fields?
推荐答案
我不确定,但是请尝试onResume()
并将文本设置为恢复状态.
i am not sure but try onResume()
and set your text in resume state.
或尝试
更改标签时的Intent.FLAG_ACTIVITY_CLEAR_TOP.
Intent.FLAG_ACTIVITY_CLEAR_TOP on tab change.
这篇关于Android Fragment中的EditText值不会刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!