Android片段中的EditText值不刷新
我正在使用Android片段中的EditText值不刷新,android,android-edittext,android-viewpager,fragment,Android,Android Edittext,Android Viewpager,Fragment,我正在使用Viewpager在3个片段之间切换,除了刷新第二个选项卡(或fragment)外,一切正常。在这个选项卡中,我有一张图片,一些静态文本视图,一些动态文本视图和一些编辑文本字段 每次选择第二个选项卡时,所有动态字段上都会调用setText()。TextView组件和微调器正在刷新和更新其内容,但EditText元素没有。 我不明白为什么这些字段没有更新。更改选项卡后,我在TabsAdapter中调用NotifyDatasetChanged()。每次我更改选项卡时,它都会调用onView
Viewpager
在3个片段之间切换,除了刷新第二个选项卡(或fragment
)外,一切正常。在这个选项卡中,我有一张图片,一些静态文本视图
,一些动态文本视图
和一些编辑文本
字段
每次选择第二个选项卡时,所有动态字段上都会调用setText()
。TextView组件和微调器正在刷新和更新其内容,但EditText元素没有。
我不明白为什么这些字段没有更新。更改选项卡后,我在TabsAdapter
中调用NotifyDatasetChanged()
。每次我更改选项卡时,它都会调用onViewCreated()
。在第二个片段的onViewCreated()
中,我正在更改元素的内容
这是我片段的代码:
@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;
}
正如我前面提到的,我的图像、TextView和微调器元素正在刷新它们的内容。我还检查了所有变量的内容,除了这些EditText元素之外,一切似乎都很好。如果我将EditText元素强制转换为TextView,则内容正在更改(在代码中,但在GUI中不更改)。让我绝望的是,在我第一次设置值时,EditText会刷新
有人知道如何刷新我的
EditText
字段的内容吗?我不确定,但请尝试onResume()
并将您的文本设置为resume状态
或尝试
Intent.FLAG_ACTIVITY_CLEAR_TOP on tab change.您还可以尝试将runnable发布到消息队列,以便在呈现后更新EditText(在MonoDroid/C中,请参阅for java):
public override视图OnCreateView(布局、充气机、视图组容器、捆绑包)
{
EditText et=FindViewById(…);
et.Post(()=>{et.Text=“content”;});
}
此外,如果您有一个TextChanged事件处理程序(例如,当文本发生更改时显示保存图标/按钮),也可以将其发布到runnable中,并在et.text赋值后执行。否则,在分配初始et.Text内容时将触发TextChanged事件,导致在用户未更改任何内容时触发TextChanged事件(即,并显示save按钮):
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();
}
public override视图OnCreateView(布局、充气机、视图组容器、捆绑包)
{
EditText et=FindViewById(…);
et.Post(()=>
{
et.Text=“内容”;
et.TextChanged+=TextChangedHandler;
});
}
私有void TextChangedHandler(对象o、事件args args)
{
ShowSaveButton();
}
THX在onResume()中设置值解决了问题。。。。但是,我不明白为什么我不能在onCreateView()中更改EditText值。如果事件处理程序被分配给相关的TextView/EditText,那么它也应该在onResume()中完成,否则缓存/更新问题也会发生。我以前认为recreate()将作为新实例完全重新运行活动,这意味着所有的视图都会被刷新并重新加载任何新的数据。然而,不幸的是,在多次重构我的代码之后,我偶然发现了官方文档和这篇文章。正如这里所建议的,在onResume()中刷新视图的内容更可靠。请确保在OnDestroy
中“注销”TextChanged
事件处理程序,否则它们可能会意外触发(例如,在重新加载视图时,尤其是TextView
在TabHost
/TabWidget
中时)。
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();
}