How to save TextView as Shared Preferences

Asked 2 years ago, Updated 2 years ago, 81 views

I saved the number of clicks on the counter using sharedpreference. If you click on it after you exit the application, the number will be counted. However, TextView is not saved, so if you press up to 3 and come back in after finishing the application, 0 appears in TextView, and if you press the button, the number continues from 4. What I want is to press 1, 2, 3 and exit the application, reconnect to 3 and press 4, 5, 6. How can I save TextView?

Private TextView counterText;
private int counter;

 private void plusCounter() {
        SharedPreferences prefs = getActivity().getSharedPreferences("counter_alone", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        counter = prefs.getInt("counter", 0);

        counter++;
        counterText.setText(counter + "");

        editor.putInt("counter", counter);
        editor.apply();
    }

android sharedpreference textview

2022-09-20 18:08

4 Answers

gist ClickerFragment.java

There may be an error because it was written on the web, not on the IDE.


2022-09-20 18:08

You cannot save the text view itself. You can save the data that goes into that view and make it by setting the initial value when the view is initialized.

As you used right after initializing TextView with findViewById or data binding, please bring the counter and setText.


2022-09-20 18:08

private TextView counterText;
private int counter;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_xxx);

    // TODO: Please initialize the shared preference.

    counter = prefs.getInt("counter", 0);
    counterText = findViewById(R.id.xxx_text_view);
    counterText.setText(counter);

}


2022-09-20 18:08

Private TextView counterText;
private Button up;
private Button reset;
private int counter;

@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container, @NonNull Bundle savedInstanceState) {
        View v= inflater. inflate(R.layout.counter_alone, container, false);

SharedPreferences prefs = getActivity().getSharedPreferences("counter_alone", Context.MODE_PRIVATE);
         SharedPreferences.Editor editor = prefs.edit();
         counter = prefs.getInt("counter", 0);



        counterText = v.findViewById(R.id.counterText);
        up = v.findViewById(R.id.up);
        reset = v.findViewById(R.id.reset);

        View.OnClickListener onClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.up:
                        counter++;
                        counterText.setText(counter + "");

                        editor.putInt("counter", counter);
                        editor.apply();

                        break;
                    case R.id.reset:
                        counter = 0;
                        counterText.setText(counter + "");

                        editor.putInt("counter", counter);
                        editor.remove("counter");
                        editor.apply();

                        break;

                }
            }
        };
        up.setOnClickListener(onClickListener);
        reset.setOnClickListener(onClickListener);

        return v;
    }
    }


2022-09-20 18:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.