0
我遇到以下情況: NavigationDrawer創建一個片段來處理用戶信息。 onCreateView()中的片段調用Web服務並返回視圖。當Web服務完成回調時,分離並附加自身的片段。當片段第二次附加onCreateView()時,應該創建一個包含用戶信息的視圖。但實際上發生的事情是視圖不會改變。重新加載片段不起作用
我錯過了什麼?
謝謝你在前進,
片段代碼:
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服務的代碼:
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();
}
}
謝謝你,它的工作。 – Carlos