Recuperacion y almacenamieento de informacion

 en esta entrada se vera un ejemplo del uso de shrap preference con el cual guardaremos y recuperaremos datos en el ambiente android studio



CODIGO.

  1. package com.example.a3;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.content.SharedPreferences;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.     private EditText etJugador, etNumero;
  14.     private TextView tvAcumulado, tvAcumulado2, tvMensaje;
  15.     private int numero;
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.  
  22.         etJugador =(EditText) findViewById(R.id.etJugador);
  23.         etNumero =(EditText) findViewById(R.id.etNumero);
  24.         tvAcumulado =(TextView) findViewById(R.id.tvAcumulado);
  25.         tvAcumulado2 =(TextView) findViewById(R.id.tvAcumulado2);
  26.         tvMensaje =(TextView) findViewById(R.id.tvmensaje);
  27.  
  28.         SharedPreferences prefe=getSharedPreferences("datos", Context.MODE_PRIVATE);
  29.         String v=String.valueOf(prefe.getInt("puntos", 0));
  30.         tvAcumulado.setText(v);
  31.         tvAcumulado2.setText(v);
  32.         numero = 1+(int)(Math.random()*20);
  33.     }
  34.  
  35.     public void Verificar(View View) {
  36.         int valor = Integer.parseInt(etNumero.getText().toString());
  37.         int jugador = Integer.parseInt(etJugador.getText().toString());
  38.  
  39.         if(jugador==1){
  40.  
  41.             if (numero == valor) {
  42.                 int puntosactual = Integer.parseInt(tvAcumulado.getText().toString());
  43.                 puntosactual = puntosactual + 1;
  44.                 tvAcumulado.setText(String.valueOf(puntosactual));
  45.                 tvMensaje.setText("¡Muy bien! ¡Gano!. Digita otro numero");
  46.  
  47.                 etNumero.setText("");
  48.  
  49.                 SharedPreferences prefe = getSharedPreferences("datos", Context.MODE_PRIVATE);
  50.                 SharedPreferences.Editor editor = prefe.edit();
  51.                 editor.putInt("puntos", puntosactual);
  52.                 editor.commit();
  53.  
  54.                 random();
  55.             }
  56.             else{
  57.                 if(valor>numero){
  58.                     tvMensaje.setText("Ingreso un numero mayor al que genro la maquina");
  59.                 }
  60.                 else{
  61.                     tvMensaje.setText("Ingreso un numero menor al que genro la maquina");
  62.                 }
  63.  
  64.             }
  65.  
  66.         }
  67.  
  68.         if(jugador==2){
  69.  
  70.             if (numero == valor) {
  71.                 int puntosactual1 = Integer.parseInt(tvAcumulado2.getText().toString());
  72.                 puntosactual1 = puntosactual1 + 1;
  73.                 tvAcumulado2.setText(String.valueOf(puntosactual1));
  74.                 tvMensaje.setText("¡Muy bien! ¡Gano!. Digita otro numero");
  75.  
  76.                 etNumero.setText("");
  77.  
  78.                 SharedPreferences prefe = getSharedPreferences("datos", Context.MODE_PRIVATE);
  79.                 SharedPreferences.Editor editor = prefe.edit();
  80.                 editor.putInt("puntos", puntosactual1);
  81.                 editor.commit();
  82.  
  83.                 random();
  84.             }
  85.             else{
  86.                 if(valor>numero){
  87.                     tvMensaje.setText("Ingreso un numero mayor al que genro la maquina");
  88.                 }
  89.                 else{
  90.                     tvMensaje.setText("Ingreso un numero menor al que genro la maquina");
  91.                 }
  92.             }
  93.         }
  94.     }
  95.  
  96.     public int random()
  97.     {
  98.         numero = 1+(int)(Math.random()*20);
  99.         return numero;
  100.     }
  101.  
  102. }

Comentarios