___ ___ erkimt Saving the value of a variable in Android Studio? ______ qstntxt ___

I am developing a small game just to friends and do some tests, the problem is I do not know practically nothing of programming and got stuck in the data rescue. I've read several times and saw some examples but could not make it work with my code. Basically I want to save the value of "Lost touch."

%pre%

}

    
______ ___ azszpr347946

try something like:

%pre%

...

%pre%     
______ ___ azszpr347992

Giving some tips more:

%pre%     
___

1

I am developing a small game just to friends and do some tests, the problem is I do not know practically nothing of programming and got stuck in the data rescue. I've read several times and saw some examples but could not make it work with my code. Basically I want to save the value of "Lost touch."

private Button botaoperder;
private TextView derrotas;
public int contador = 0;


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

    botaoperder= findViewById(R.id.botao1);
    derrotas= findViewById(R.id.derrotas00);

    botaoperder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador++;
            derrotas.setText("Derrotas por toques: " + contador);


        }
    });



}

}

    
asked by anonymous 05.12.2018 / 00:12

2 answers

1
___ ___ erkimt Saving the value of a variable in Android Studio? ______ qstntxt ___

I am developing a small game just to friends and do some tests, the problem is I do not know practically nothing of programming and got stuck in the data rescue. I've read several times and saw some examples but could not make it work with my code. Basically I want to save the value of "Lost touch."

import android.content.SharedPreferences;

}

    
______ ___ azszpr347946

try something like:

private Button botaoperder; 
private TextView derrotas;
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
public int contador = 0;


@Override
protected void onCreate(final Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    preferences = getSharedPreferences("JogoToque", MODE_PRIVATE);

    editor = preferences.edit();

    botaoperder = findViewById(R.id.botao1);
    derrotas    = findViewById(R.id.derrotas00);

    contador = preferences.getInt("derrotas", 0);
    derrotas.setText("Derrotas por toques: " + contador);

    botaoperder.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        contador++;
        derrotas.setText("Derrotas por toques: " + contador);
        editor.putInt("derrotas", contador);
        editor.commit();
      }
    });
}

...

import android.content.SharedPreferences;
    
______ ___ azszpr347992

Giving some tips more:

private Button botaoperder; 
private TextView derrotas;
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
public int contador = 0;


@Override
protected void onCreate(final Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    preferences = getSharedPreferences("JogoToque", MODE_PRIVATE);

    editor = preferences.edit();

    botaoperder = findViewById(R.id.botao1);
    derrotas    = findViewById(R.id.derrotas00);

    contador = preferences.getInt("derrotas", 0);
    derrotas.setText("Derrotas por toques: " + contador);

    botaoperder.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        contador++;
        derrotas.setText("Derrotas por toques: " + contador);
        editor.putInt("derrotas", contador);
        editor.commit();
      }
    });
}
    
___
05.12.2018 / 02:59
0
___ ___ erkimt Saving the value of a variable in Android Studio? ______ qstntxt ___

I am developing a small game just to friends and do some tests, the problem is I do not know practically nothing of programming and got stuck in the data rescue. I've read several times and saw some examples but could not make it work with my code. Basically I want to save the value of "Lost touch."

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

  //Aqui é onde suas variaveis são "carregadas"
    preferences = getSharedPreferences("JogoToque", MODE_PRIVATE);
//Editor que permite você modificar os dados
    editor = preferences.edit();

    botaoperder = findViewById(R.id.botao1);
    derrotas    = findViewById(R.id.derrotas00);
//Aqui você atribui a sua variável contador o valor anteriormente salvo, através da key "derrotas"
    contador = preferences.getInt("derrotas", 0);
    derrotas.setText("Derrotas por toques: " + contador);

    botaoperder.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        contador++;
        derrotas.setText("Derrotas por toques: " + contador);

      //Aqui você atribui a key "derrotas" o valor contido na int contador 
        editor.putInt("derrotas", contador);

      //Aqui você de fato "salva"
        editor.commit();
      }
    });
}

}

    
______ ___ azszpr347946

try something like:

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

  //Aqui é onde suas variaveis são "carregadas"
    preferences = getSharedPreferences("JogoToque", MODE_PRIVATE);
//Editor que permite você modificar os dados
    editor = preferences.edit();

    botaoperder = findViewById(R.id.botao1);
    derrotas    = findViewById(R.id.derrotas00);
//Aqui você atribui a sua variável contador o valor anteriormente salvo, através da key "derrotas"
    contador = preferences.getInt("derrotas", 0);
    derrotas.setText("Derrotas por toques: " + contador);

    botaoperder.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        contador++;
        derrotas.setText("Derrotas por toques: " + contador);

      //Aqui você atribui a key "derrotas" o valor contido na int contador 
        editor.putInt("derrotas", contador);

      //Aqui você de fato "salva"
        editor.commit();
      }
    });
}

...

%pre%     
______ ___ azszpr347992

Giving some tips more:

%pre%     
___
05.12.2018 / 13:11