Save data on button click using Shared Preferences

Viewed 63

I am working on an activity, my goal is that by clicking the save button it shows me the data, entered values and clicking the edit button allows me to edit the data, saved values. I have some TextView and EditText and 2 buttons.

´´´
public class MyInfo extends AppCompatActivity {
    SharedPreferences sharedpreferences;
    TextView name;
    TextView address;
    TextView firstContactName;
    TextView firstContactPhoneNumber;
    TextView secondContactName;
    TextView secondContactPhoneNumber;
    public static final String mypreference = "mypref";
    public static final String Name = "nameKey";
    public static final String Address = "addressKey";
    public static final String FirstContactName = "firstContactNameKey";
    public static final String SecondContactName = "secondContactNameKey";
    public static final String FirstContactPhoneNumber = "firstContactPhoneNumberKey";
    public static final String SecondContactPhoneNumber = "secondContactPhoneNumber";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);
        name = (TextView) findViewById(R.id.edit_text_my_name);
        address = (TextView) findViewById(R.id.edit_text_my_address);
        firstContactName = (TextView) findViewById(R.id.edit_text_first_contact_name);
        firstContactPhoneNumber = (TextView) findViewById(R.id.edit_text_first_contact_phone);
        secondContactName = (TextView) findViewById(R.id.edit_text_second_contact_name);
        secondContactPhoneNumber = (TextView) findViewById(R.id.edit_text_second_contact_phone);
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);
        if (sharedpreferences.contains(Name)) {
            name.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Address)) {
            address.setText(sharedpreferences.getString(Address, ""));
        }
        if (sharedpreferences.contains(FirstContactName)) {
            firstContactName.setText(sharedpreferences.getString(FirstContactName, ""));
        }
        if (sharedpreferences.contains(FirstContactPhoneNumber)) {
            firstContactPhoneNumber.setText(sharedpreferences.getString(FirstContactPhoneNumber, ""));
        }
        if (sharedpreferences.contains(SecondContactName)) {
            secondContactName.setText(sharedpreferences.getString(SecondContactName, ""));
        }
        if (sharedpreferences.contains(SecondContactPhoneNumber)) {
            secondContactPhoneNumber.setText(sharedpreferences.getString(SecondContactPhoneNumber, ""));
        }
    }

    public void Save(View view) {

        String n = name.getText().toString();
        String a = address.getText().toString();
        String fcn = firstContactName.getText().toString();
        String fcpn = firstContactPhoneNumber.getText().toString();
        String scn = secondContactName.getText().toString();
        String scpn = secondContactPhoneNumber.getText().toString();
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(Name, n);
        editor.putString(Address, a);
        editor.putString(FirstContactName, fcn);
        editor.putString(FirstContactPhoneNumber, fcpn);
        editor.putString(SecondContactName, scn);
        editor.putString(SecondContactPhoneNumber, scpn);
        editor.apply();

        name = (TextView) findViewById(R.id.edit_text_my_name);
        address = (TextView) findViewById(R.id.edit_text_my_address);
        firstContactName = (TextView) findViewById(R.id.edit_text_first_contact_name);
        firstContactPhoneNumber = (TextView) findViewById(R.id.edit_text_first_contact_phone);
        secondContactName = (TextView) findViewById(R.id.edit_text_second_contact_name);
        secondContactPhoneNumber = (TextView) findViewById(R.id.edit_text_second_contact_phone);
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);

        if (sharedpreferences.contains(Name)) {
            name.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Address)) {
            address.setText(sharedpreferences.getString(Address, ""));
        }
        if (sharedpreferences.contains(FirstContactName)) {
            firstContactName.setText(sharedpreferences.getString(FirstContactName, ""));
        }
        if (sharedpreferences.contains(FirstContactPhoneNumber)) {
            firstContactPhoneNumber.setText(sharedpreferences.getString(FirstContactPhoneNumber, ""));
        }
        if (sharedpreferences.contains(SecondContactName)) {
            secondContactName.setText(sharedpreferences.getString(SecondContactName, ""));
        }
        if (sharedpreferences.contains(SecondContactPhoneNumber)) {
            secondContactPhoneNumber.setText(sharedpreferences.getString(SecondContactPhoneNumber, ""));
        }
    }
}
´´´

I have thought about it but I can't make it work, I don't know what is wrong with my java code

´´´

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/padding_left_right"
    android:paddingRight="@dimen/padding_left_right"
    android:paddingTop="@dimen/padding_top_bottom"
    android:paddingBottom="@dimen/padding_top_bottom"
    android:orientation="vertical"
    tools:context=".MyInfo">


    <TextView
        android:id="@+id/text_view_my_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_name"
        android:textAlignment="center"
        android:layout_gravity="center_horizontal"/>

    <EditText
        android:id="@+id/edit_text_my_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text_view_my_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_address"
        android:textAlignment="center"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:id="@+id/edit_text_my_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text_view_first_contact_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/first_contact_name"
        android:textAlignment="center"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:id="@+id/edit_text_first_contact_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text_view_first_contact_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/first_contact_phone"
        android:textAlignment="center"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:id="@+id/edit_text_first_contact_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text_view_second_contact_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/second_contact_name"
        android:textAlignment="center"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:id="@+id/edit_text_second_contact_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text_view_second_contact_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/second_contact_phone"
        android:textAlignment="center"
        android:layout_gravity="center_horizontal" />

    <EditText
        android:id="@+id/edit_text_second_contact_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center">

        <Button
            android:id="@+id/edit_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="Clear"
            android:text="@string/edit_button" />

        <Button
            android:id="@+id/save_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="Save"
            android:text="@string/save_button" />

    </LinearLayout>

</LinearLayout>

´´´

2 Answers

first initial your widget then save values on share preference

 if (sharedpreferences.contains(Name)) {
        name.setText(sharedpreferences.getString(Name, ""));
    }
    if (sharedpreferences.contains(Address)) {
        address.setText(sharedpreferences.getString(Address, ""));
    }
    if (sharedpreferences.contains(FirstContactName)) {
        firstContactName.setText(sharedpreferences.getString(FirstContactName, ""));
    }
    if (sharedpreferences.contains(FirstContactPhoneNumber)) {
        firstContactPhoneNumber.setText(sharedpreferences.getString(FirstContactPhoneNumber, ""));
    }
    if (sharedpreferences.contains(SecondContactName)) {
        secondContactName.setText(sharedpreferences.getString(SecondContactName, ""));
    }
    if (sharedpreferences.contains(SecondContactPhoneNumber)) {
        secondContactPhoneNumber.setText(sharedpreferences.getString(SecondContactPhoneNumber, ""));
    }

 name = (TextView) findViewById(R.id.edit_text_my_name);
    address = (TextView) findViewById(R.id.edit_text_my_address);
    firstContactName = (TextView) findViewById(R.id.edit_text_first_contact_name);
    firstContactPhoneNumber = (TextView) findViewById(R.id.edit_text_first_contact_phone);
    secondContactName = (TextView) findViewById(R.id.edit_text_second_contact_name);
    secondContactPhoneNumber = (TextView) findViewById(R.id.edit_text_second_contact_phone);
    sharedpreferences = getSharedPreferences(mypreference,
            Context.MODE_PRIVATE);


  String n = name.getText().toString();
    String a = address.getText().toString();
    String fcn = firstContactName.getText().toString();
    String fcpn = firstContactPhoneNumber.getText().toString();
    String scn = secondContactName.getText().toString();
    String scpn = secondContactPhoneNumber.getText().toString();
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putString(Name, n);
    editor.putString(Address, a);
    editor.putString(FirstContactName, fcn);
    editor.putString(FirstContactPhoneNumber, fcpn);
    editor.putString(SecondContactName, scn);
    editor.putString(SecondContactPhoneNumber, scpn);
    editor.apply();

All your text strings seems to be coming from 'TextView'. I can't see the 'EditText' where you would edit the values. Change the values you want to be editable to be displayed in 'EditText' instead of 'TextView'.

Related