I am getting response from API and I want to update profile. When I click on update profile. So updated data is not showing in text box. Because i am getting data from shared preference. after getting data i want to show updated data in text field. So I am trying to update shared preference live. Can I fix this issue using clear model data? and how?
public class EditProfile extends Fragment implements Consumes {
@Inject
ViewModelFactorys viewModelFactorys;
Profileviewmodel profileviewmodel;
EditText TVname;
EditText TVemail;
EditText TVmobile;
TextView tvname;
TextView tvemail;
TextView tvdepartment;
Button update;
private Loginmodel loginmodel;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_edit_profile, container, false);
loginmodel = new Gson().fromJson(SharedPref.Getloggeduserdetials(SharedPref.LOGGOEDUSERDATABASE,""), Loginmodel.class);
Log.d("Login Data", loginmodel.toString());
profileviewmodel = ViewModelProviders.of(getActivity(), viewModelFactorys).get(Profileviewmodel.class);
profileviewmodel.apiResponse().observe(this, this::consumeResponsess);
update = rootView.findViewById(R.id.update);
TVname = (EditText)rootView.findViewById(R.id.TVname);
TVemail = (EditText)rootView.findViewById(R.id.TVemail);
TVmobile = (EditText)rootView.findViewById(R.id.TVmobile);
tvname = rootView.findViewById(R.id.tv_name);
tvemail = rootView.findViewById(R.id.tv_email);
tvdepartment = rootView.findViewById(R.id.tv_type);
TVname.setText(loginmodel.getName());
TVemail.setText(loginmodel.getEmail());
TVmobile.setText(loginmodel.getPhone());
tvname.setText(loginmodel.getName());
tvemail.setText(loginmodel.getPhone());
tvdepartment.setText(loginmodel.getDepartment());
update.setOnClickListener(view ->{
if (!TVname.getText().toString().isEmpty()) {
if (!TVemail.getText().toString().isEmpty()) {
Utility.showProgressdialog(getContext(), "");
Log.d("user name",TVname.getText().toString());
profileviewmodel.updateprofile(TVname.getText().toString().trim(),TVemail.getText().toString().trim(),SharedPref.Gettoken(SharedPref.TOKEN,""),getActivity());
} else
Snackbar.make(view, "Please enter Name", Snackbar.LENGTH_LONG).show();
} else
Snackbar.make(view, "Please enter email address.", Snackbar.LENGTH_LONG).show();
});
return rootView;
}
@Override
public void handleresponse(ApiResponse apiResponse) {
if (apiResponse.data.getSuccess().equalsIgnoreCase("1")){
if (apiResponse.data.getData() instanceof Loginmodel) {
loginmodel = (Loginmodel) apiResponse.data.getData();
SharedPref.Saveloggeduserdetails(SharedPref.LOGGOEDUSERDATABASE, new Gson().toJson(loginmodel));
}
}
}}
public static void Saveloggeduserdetails(String key, String value) {
SharedPreferences.Editor prefsEditor = mSharedPref.edit();
prefsEditor.putString(key, value);
prefsEditor.commit();
}