如何更新不显示片段
How to update not showing fragments
我正在开发一个 Android 应用程序,它不时从本地服务器接收信息。对于应用内导航,我选择使用导航抽屉,它用
替换选定的片段
getSupportFragmentManager().beginTransaction().replace(R.id.flContent, fragment).addToBackStack(null).commit();
将选定的片段添加到 backStack 以实现正确的 BackPress 导航。
片段在我的MainActivity中初始化
private OverviewFragment overviewFragment;
private IncidentFragment incidentFragment;
private SettingsFragment settingsFragment;
protected void onCreate(){
...
overviewFragment = new OverviewFragment();
incidentFragment = new IncidentFragment();
settingsFragment = new SettingsFragment();
...
}
我现在要做的是根据从服务器接收到的信息更新 TextView。我尝试了以下方法:
public void setIncidentDetails(NetworkIncident incident) {
if (incident != null) {
ArrayList<String> medicNames = null;
((TextView) view.findViewById(R.id.inLocationText)).setText(incident.getLocation());
((TextView) view.findViewById(R.id.inKeywordText)).setText(incident.getKeyword());
if(incident.isEmsCalled()) {
((TextView) view.findViewById(R.id.inEmsText)).setText("Rettungsdienst alarmiert");
} else {
((TextView) view.findViewById(R.id.inEmsText)).setText("Kein Rettungsdienst alarmiert");
}
for (int i = 0; i < incident.getMedics().size(); i++){
medicNames = new ArrayList<String>();
NetworkMedic currentMedic = incident.getMedics().get(i);
medicNames.add(currentMedic.getForename() + " " + currentMedic.getSurename());
}
((TextView) view.findViewById(R.id.inMedicText)).setText(medicNames.toString());
}
}
这仅在我之前打开包含 TextView 的片段时有效。否则我会在“view.findViewById...”上得到 NullPointerException。很明显是因为之前没有初始化过 ;)
我的问题是,如果片段未显示(在 NavigatinoDrawer 中选择了另一个片段)或者甚至没有被选择,是否还有其他方法可以更新 TextView。
(可能是片段从中提取信息的一些全局资源文件 onCreate / onResume)?
感谢@Tejas 我可以解决这个问题。您确实必须使用数据库或临时存储要更新的数据来更新当前未显示的片段。
一旦此片段进入前台,您就可以检索存储的数据以更新您的 TextView 等...
我正在开发一个 Android 应用程序,它不时从本地服务器接收信息。对于应用内导航,我选择使用导航抽屉,它用
替换选定的片段getSupportFragmentManager().beginTransaction().replace(R.id.flContent, fragment).addToBackStack(null).commit();
将选定的片段添加到 backStack 以实现正确的 BackPress 导航。
片段在我的MainActivity中初始化
private OverviewFragment overviewFragment;
private IncidentFragment incidentFragment;
private SettingsFragment settingsFragment;
protected void onCreate(){
...
overviewFragment = new OverviewFragment();
incidentFragment = new IncidentFragment();
settingsFragment = new SettingsFragment();
...
}
我现在要做的是根据从服务器接收到的信息更新 TextView。我尝试了以下方法:
public void setIncidentDetails(NetworkIncident incident) {
if (incident != null) {
ArrayList<String> medicNames = null;
((TextView) view.findViewById(R.id.inLocationText)).setText(incident.getLocation());
((TextView) view.findViewById(R.id.inKeywordText)).setText(incident.getKeyword());
if(incident.isEmsCalled()) {
((TextView) view.findViewById(R.id.inEmsText)).setText("Rettungsdienst alarmiert");
} else {
((TextView) view.findViewById(R.id.inEmsText)).setText("Kein Rettungsdienst alarmiert");
}
for (int i = 0; i < incident.getMedics().size(); i++){
medicNames = new ArrayList<String>();
NetworkMedic currentMedic = incident.getMedics().get(i);
medicNames.add(currentMedic.getForename() + " " + currentMedic.getSurename());
}
((TextView) view.findViewById(R.id.inMedicText)).setText(medicNames.toString());
}
}
这仅在我之前打开包含 TextView 的片段时有效。否则我会在“view.findViewById...”上得到 NullPointerException。很明显是因为之前没有初始化过 ;)
我的问题是,如果片段未显示(在 NavigatinoDrawer 中选择了另一个片段)或者甚至没有被选择,是否还有其他方法可以更新 TextView。
(可能是片段从中提取信息的一些全局资源文件 onCreate / onResume)?
感谢@Tejas 我可以解决这个问题。您确实必须使用数据库或临时存储要更新的数据来更新当前未显示的片段。
一旦此片段进入前台,您就可以检索存储的数据以更新您的 TextView 等...