0

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();
    }
  • 1
    Perhaps share the code for the Saveloggeduserdetails() method? As well is the code where you set the text for the text box. – Zee Jul 15 '20 at 09:21
  • 1
    @Zee public static void Saveloggeduserdetails(String key, String value) { SharedPreferences.Editor prefsEditor = mSharedPref.edit(); prefsEditor.putString(key, value); prefsEditor.commit(); } – Arpit Singh Jul 15 '20 at 09:27
  • So you want to update the textview without using the SharedPrefs? Is this textview in a different fragment or a different Activity? – Eliza Camber Jul 15 '20 at 09:31
  • hmm ok, you may want to consider using prefsEditor.apply() instead as stated here: https://stackoverflow.com/questions/5960678/whats-the-difference-between-commit-and-apply-in-sharedpreferences#:~:text=Unlike%20commit()%2C%20which%20writes,be%20notified%20of%20any%20failures. BUT that might not solve your issue. Please post the code where you set the textView value - your issue might be here instead. – Zee Jul 15 '20 at 09:31
  • @ElizaCamber no, I want to update using shared preference. because if update shared preference so data will update on all place. – Arpit Singh Jul 15 '20 at 09:32
  • @Zee I have tried apply() also but not working. – Arpit Singh Jul 15 '20 at 09:33
  • Well then the problem is most likely where you retrieve the data from sharedPrefs and display it. Please post the code? – Zee Jul 15 '20 at 09:36
  • I am not sure, but you can not change data in a textbox, by just updating data in shared preference. – Jaydeep chatrola Jul 15 '20 at 09:41
  • 1
    @Zee I have updated – Arpit Singh Jul 15 '20 at 09:42

3 Answers3

0

do like this or you have to use livedata and observe it in activity

@Override
public void handleresponse(ApiResponse apiResponse) {


    if (apiResponse.data.getSuccess().equalsIgnoreCase("1")){
        if (apiResponse.data.getData() instanceof Loginmodel) {
            loginmodel = (Loginmodel) apiResponse.data.getData();

              TVname.setText(loginmodel.getName());
              TVemail.setText(loginmodel.getEmail());
              TVmobile.setText(loginmodel.getPhone());

              tvname.setText(loginmodel.getName());
              tvemail.setText(loginmodel.getPhone());
              tvdepartment.setText(loginmodel.getDepartment());
            SharedPref.Saveloggeduserdetails(SharedPref.LOGGOEDUSERDATABASE, new Gson().toJson(loginmodel));

        }

    }
}}
Jaydeep chatrola
  • 2,423
  • 11
  • 17
0

Put the set text methods in a separate method instead of the onCreateView like this:

private void updateScreen() {
    TVname.setText(loginmodel.getName());
    TVemail.setText(loginmodel.getEmail());
    TVmobile.setText(loginmodel.getPhone());

    tvname.setText(loginmodel.getName());
    tvemail.setText(loginmodel.getPhone());
    tvdepartment.setText(loginmodel.getDepartment());
}

then when you update the shared prefs call that method:

public static void Saveloggeduserdetails(String key, String value) {
   SharedPreferences.Editor prefsEditor = mSharedPref.edit();
   prefsEditor.putString(key, value);
   prefsEditor.commit();
   updateScreen();
}
Eliza Camber
  • 1,536
  • 1
  • 13
  • 21
0

There could be multiple ways this could have happened. You have not provided the full code of HOW exactly you store your shared preferences. For example, 'prefsEditor' might be incorrect. Depending on how you access it every time. Here is some example code I've used to store/retrieve Shared prefs:

public void setLoginData (String username, String password) {
        SharedPreferences sharedPrefs = getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPrefs.edit();

        editor.putString(PREF_FILE_USERNAME_EXT, username);
        editor.putString(PREF_FILE_PASSWORD_EXT, password);
        editor.apply();
    }

and then

   public String getUsername() {
        return getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE)
                .getString(PREF_FILE_USERNAME_EXT, "");
    }

(As an additional note, if you are using a singleton, be careful as the OS might actually delete the values of the singleton without you knowing. For shared preferences, for now at least, access it like above to first determine if your shared preferences is working properly.)

Another issue could be that your JSON is incorrect or not working. Do a printline before you save to shared instance to make sure you ARE actually saving stuff. What does Log.d("Login Data", loginmodel.toString()); print out in your code? Is it correct?

Zee
  • 1,592
  • 12
  • 26