Save Usage Preferences - Android

Asked

Viewed 186 times

0

I would like to know the best way to save the usage preferences of an application, such as a checkbox marked or the ordering of a listview. if you can pass some example code thank you.

  • can use the Shared preferences or the Api Preference It depends on what and how you want to do it. It’s been a while since I’ve touched android so I don’t have code.

  • Welcome to Stackoverflow! Make a tour to learn how the site works. Regarding your question it seems to me that more information is missing, show what you have already done and what the difficulties are facing, edit it and add more information about the problem.

1 answer

2

Creates the Sharedpreferences object

SharedPreferences pref = getApplicationContext().getSharedPreferences("MinhasPreferencias", MODE_PRIVATE); 
Editor editor = pref.edit();

Saving the data in pairs KEY/VALUE

editor.putBoolean("nome_da_chave1", true);      // Salvando valor booleano - true/false
editor.putInt("nome_da_chave2", "int value");   // Salvando valor inteiro integer
editor.putFloat("nome_chave3", "float value");  // Salvando float
editor.putLong("nome_chave4", "long value");    // Salvando long
editor.putString("nome_chave5", "string value");  // Salvando string

// Salva as alterações no objeto SharedPreferences
editor.commit();   // valida as alterações

Reading the data from Sharedpreferences

boolean estachecado = pref.getBoolean("nome_chave1", true);  // lendo o  boolean
int qualaidade = pref.getInt("nome_chave2", 0);             // lendo o valor inteiro
float total =pref.getFloat("nome_chave3", null);        // lendo o valor  Float
long distancia = pref.getLong("nome_chave4", null);     // lendo o valor Long
String email=pref.getString("nome_chave5", null);       // lendo a String

Excluding values from Sharedpreferences

editor.remove("nome_chave3"); // excluindo a chave nome_chave3
editor.remove("nome_chave4"); // excluindo a chave nome_chave4

// Salva as alterações em SharedPreferences
editor.commit(); // valida as mudanças

Eliminates all data from Sharedpreferences

 editor.clear();
 editor.commit(); // Salva as alterações
  • Perfect Reginaldo, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.