1

Estoy trabajando en una activty, mi objetivo es que al hacer clic en el botón guardar me muestre los datos, me devuelva los datos guardados en sharedpreferences y no me permita su edicion. Y al hacer click en el boton editar poder editarlos para volver a guardarlos. La activity cuenta de algunos TextView y EditView y los 2 respectivos botones de editar y guardar.

No estoy logrando que funcione.


import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

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, ""));
        }

    }
}

Les dejo el codigo Java en caso de que alguien pueda orientarme para ver en que estoy fallando, si veo que me los guarda pero a su vez me permite la edicion y no es lo que busco


    <?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>

1 respuesta 1

0

Primeramente no necesitas obtener las referencias de los EditText dentro de tu mètodo Save() ya que lo realizaste inicialmente en el mètodo onCreate().

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();


}

Ahora despuès de guardar los valores en preferencias al iniciar tu app debes obtenerlos de la siguiente forma, ùnicamente buscando el valor en la preferencias usando su llave asignada, si no existe estas definiendo que el valor que agregarà en el TextView serìa un String vacìo:

            @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);
            
                    
                    name.setText(sharedpreferences.getString(Name, ""));
                    address.setText(sharedpreferences.getString(Address, ""));
                    firstContactName.setText(sharedpreferences.getString(FirstContactName, ""));
firstContactPhoneNumber.setText(sharedpreferences.getString(FirstContactPhoneNumber, ""));
                 secondContactName.setText(sharedpreferences.getString(SecondContactName, ""));
secondContactPhoneNumber.setText(sharedpreferences.getString(SecondContactPhoneNumber, ""));
            
                }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.