How to use a sharePreferences with a tableLayout

1

I am creating an application that subtracts two dates from me and shows me the number of days, but the result I want to show it to me in another layout. the sharePrefereneces could show me two results in a linear way, but I want to keep the day, month, year of beginning and the result of the days in the same line ... I do not know if there is another way to obtain what I need. Thanks for the help

Año1 = (EditText) findViewById(R.id.etAñoInicio);
        Mes1 = (EditText) findViewById(R.id.etMesInicio);
        Dia1 = (EditText) findViewById(R.id.etDiaInicio);
        Año2 = (EditText) findViewById(R.id.etAñoFin);
        Mes2 = (EditText) findViewById(R.id.etMesFin);
        Dia2 = (EditText) findViewById(R.id.etDiaFin);
        resultado = (EditText) findViewById(etTotal3);
        btnGuardar = (Button) findViewById(R.id.btnGuardarT3);
        Lista = (TableLayout) findViewById(R.id.ListaT3);

        Calendar C2 = Calendar.getInstance();
        a1 = C2.get(Calendar.YEAR);
        m1 = C2.get(Calendar.MONTH) +1;
        d1 = C2.get(Calendar.DAY_OF_MONTH);

        a2 = C2.get(Calendar.YEAR);
        m2 = C2.get(Calendar.MONTH) +1;
        d2 = C2.get(Calendar.DAY_OF_MONTH);

        Dia1.setText(""+d1);
        Mes1.setText(""+m1);
        Año1.setText(""+a1);
        Dia2.setText(""+d1);
        Mes2.setText(""+m1);
        Año2.setText(""+a1);

        a1 = Integer.valueOf(Año1.getText().toString());
        a2 = Integer.valueOf(Año2.getText().toString());
        m1 = Integer.valueOf(Mes1.getText().toString());
        m2 = Integer.valueOf(Mes2.getText().toString());
        d1 = Integer.valueOf(Dia1.getText().toString());
        d2 = Integer.valueOf(Dia2.getText().toString());

        rA = a2 - a1;
        rM = m2 - m1;
        rD = d2 - d1;

        Rt = (rA*360)+(rM*30)+(rD+1);

        resultado.setText(""+Rt);


        btnGuardar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                a1 = Integer.valueOf(Año1.getText().toString());
                a2 = Integer.valueOf(Año2.getText().toString());
                m1 = Integer.valueOf(Mes1.getText().toString());
                m2 = Integer.valueOf(Mes2.getText().toString());
                d1 = Integer.valueOf(Dia1.getText().toString());
                d2 = Integer.valueOf(Dia2.getText().toString());

                rA = a2 - a1;
                rM = m2 - m1;
                rD = d2 - d1;

                Rt = (rA*360)+(rM*30)+(rD+1);

                resultado.setText(""+Rt);

                String [] cadena={"Inicio: ", Dia1.getText().toString()+ " / ", Mes1.getText().toString()+ " / ",Año1.getText().toString()+ "   Dias: ",resultado.getText().toString()};
                TableLayout tabla=(TableLayout)findViewById(R.id.ListaT3);
                TextView textView;
                //abrimos el table row agregar las filas
                TableRow row=new TableRow(getBaseContext());
                for(int i=0;i<5;i++){

                    textView = new TextView(getBaseContext());

                    textView.setGravity(Gravity.CENTER_HORIZONTAL);

                    textView.setPadding(10, 5, 30, 30);

                    textView.setText(cadena[i]);

                    textView.setTextColor(Color.BLUE);

                    row.addView(textView);
                }

                tabla.addView(row);

            }

        });

    }

}

// What I need is that what is shown to me here is stored in the internal memory and shown to me, the same in another layout .... P.D: do not use databases: (

    
asked by Nestor Bogantes rdz 27.11.2017 в 15:28
source

1 answer

1

First create the methods to save and obtain the preference values:

public void guardarDato(Context context, String valorKey, String valor) {
    SharedPreferences settings;
    SharedPreferences.Editor editor;
    settings = PreferenceManager.getDefaultSharedPreferences(context);
    editor = settings.edit();
    editor.putString(valorKey, valor);
    editor.commit();
}

public String obtenerDato(Context context, valorKey) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return  preferences.getString(valorKey, "");
}

Now using the guardarDato() method you can save the day, month and year values in the preference:

guardarDato(getApplicationContext(), "dia", Dia1.getText().toString());
guardarDato(getApplicationContext(), "mes", Mes1.getText().toString());
guardarDato(getApplicationContext(), "ano", Año1.getText().toString());

Now as an example to obtain the value of the day, we obtain it by means of the "key" that we define, in this case "day", "month", "year":

String valorDia = obtenerDato(getApplicationContext(), "dia");
String valorMes = obtenerDato(getApplicationContext(), "mes");
String valorAno = obtenerDato(getApplicationContext(), "ano");

This way you can get the values that were saved in the preference and use them in another part of the application.

    
answered by 27.11.2017 / 21:14
source