我有以下情况: NavigationDrawer创建一个Fragment来处理用户信息。 onCreateView()上的片段调用Web Service并返回View。当Web服务完成回调分离和附加自身的片段。第二次在onCreateView()上附加片段时,应创建一个包含用户信息的视图。但实际上发生的事情是View不会改变。
我错过了什么?
提前谢谢你,
片段代码:
private boolean mIsRefresh = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Gets Application Context
mContext = getActivity().getApplicationContext();
// Set up environment to use DataBase
mGlobal = (Global) getActivity().getApplicationContext();
mDAOFactory = mGlobal.getDAOFactory();
mFragment = this;
mFragmentView = inflater.inflate(R.layout.fragment_user_info, container, false);
buildView();
if (mIsRefresh) {
return mFragmentView;
}else {
return null;
}
}
这是buildView()代码:
private void buildView() {
mEtUserName = (EditText) mFragmentView.findViewById(R.id.userName);
mEtUserEmail = (EditText) mFragmentView.findViewById(R.id.userEmail);
mEtPassword = (EditText) mFragmentView.findViewById(R.id.password);
mEtPasswordConfirmation = (EditText) mFragmentView.findViewById(R.id.passwordConfirmation);
mIbSendPassword = (ImageButton) mFragmentView.findViewById(R.id.sendPassword);
mAcUserCountry = (AutoCompleteTextView) mFragmentView.findViewById(R.id.userCountry);
mEtUserState = (EditText) mFragmentView.findViewById(R.id.userState);
mEtUserCity = (EditText) mFragmentView.findViewById(R.id.userCity);
mEtUserPhoneNumberCountry = (EditText) mFragmentView.findViewById(R.id.userPhoneNumberCountry);
mEtUserPhoneNumberCity = (EditText) mFragmentView.findViewById(R.id.userPhoneNumberCity);
mEtUserPhoneNumber = (EditText) mFragmentView.findViewById(R.id.userPhoneNumber);
mCbTCAccepted = (CheckBox) mFragmentView.findViewById(R.id.cbTCAccepted);
// User has not been created
if (mGlobal.getUserUID() == 0) {
getUserEmail();
getUserLocation();
} else {
if (mIsRefresh) {
formatView();
} else {
getDataOnServer();
}
}
}
以下是调用Web Service的代码:
private void getDataOnServer () {
mUserDAO = mDAOFactory.getUserDAO(mContext);
final ProgressDialog progressDialog = ProgressDialog.show (getActivity(),
getString(R.string.msgConnectingServer), getString(R.string.msgGettingData), true);
//TODO Retirar o set abaixo
mGlobal.setUserUID(1);
mUserDAO.getUserByUID(mGlobal.getUserUID(), new Callback<User>() {
@Override
public void success(User user, Response response) {
mUser = user;
mIsRefresh = true;
refresh();
}
@Override
public void failure(RetrofitError error) {
mUser = null;
Log.e(getClass().getName(), "Retrofit Error = " + error);
refresh();
}
private void refresh() {
progressDialog.dismiss();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.detach(mFragment)
.attach(mFragment)
.commit();
}
});
}
最后是onStart()
@Override
public void onStart() {
super.onStart();
if (mIsRefresh) {
mIsRefresh = false;
getFragmentManager().beginTransaction()
.remove(mFragment)
.add(R.id.content_frame, mFragment)
.commit();
}
}
答案 0 :(得分:2)
而不是
[packed=true]
这样做:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.detach(mFragment).attach(mFragment).commit();