I had to see a tuturial on youtube and I copied this code to save and delete inserted values, my doubt is that it always appears that "0" as it has below and I would like to know how I can remove it without giving error, I leave also the code and of course if there is another easier way to use shared preferences.
.....int val1 = myprefs.getInt("keybanca",0);
banca.setText(String.valueOf(val1));.....
----------------------------</>-------------------------------
public class Gestao extends AppCompatActivity {
EditText banca, greens, reds, lucro, prejuizo, total, num1, num2, ano;
TextView total2;
SharedPreferences myprefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gestao);
myprefs = getPreferences(MODE_PRIVATE);
init();
}
public void back2(View view) {
Intent back= new Intent(this, Menu.class);
startActivity(back);
}
private void init() {
banca = (EditText) findViewById(R.id.editText);
greens = (EditText) findViewById(R.id.editText2);
reds = (EditText) findViewById(R.id.editText3);
lucro = (EditText) findViewById(R.id.editText4);
prejuizo = (EditText) findViewById(R.id.editText5);
total = (EditText) findViewById(R.id.editText6);
num1 = (EditText) findViewById(R.id.editText13);
num2 = (EditText) findViewById(R.id.editText14);
total2 = (TextView) findViewById(R.id.textView16);
ano = (EditText) findViewById(R.id.editText21);
readPreferences();
}
public void onSave(View view){
int bancaText = Integer.parseInt(banca.getText().toString());
int greensText = Integer.parseInt(greens.getText().toString());
int redsText = Integer.parseInt(reds.getText().toString());
int lucroText = Integer.parseInt(lucro.getText().toString());
int prejuizoText = Integer.parseInt(prejuizo.getText().toString());
int totalText = Integer.parseInt(total.getText().toString());
int anoText = Integer.parseInt(ano.getText().toString());
SharedPreferences.Editor editor = myprefs.edit();
editor.putInt("keybanca", bancaText);
editor.putInt("keygreens", greensText);
editor.putInt("keyreds", redsText);
editor.putInt("keylucro", lucroText);
editor.putInt("keyprejuizo", prejuizoText);
editor.putInt("keytotal",totalText);
editor.putInt("keyano",anoText);
editor.commit ();
Toast.makeText(getApplicationContext(), "Guardado",
Toast.LENGTH_LONG).show();
}
public void onReset (View view) {
SharedPreferences.Editor editor = myprefs.edit();
editor.clear();
editor.commit();
readPreferences();
}
public void readPreferences()
{
int val1 = myprefs.getInt("keybanca",0);
banca.setText(String.valueOf(val1));
int val2 = myprefs.getInt("keygreens",0);
greens.setText(String.valueOf(val2));
int val3 = myprefs.getInt("keyreds", 0);
reds.setText(String.valueOf(val3));
int val4 = myprefs.getInt("keylucro",0);
lucro.setText(String.valueOf(val4));
int val5 = myprefs.getInt("keyprejuizo",0);
prejuizo.setText(String.valueOf(val5));
int val6 = myprefs.getInt("keytotal", 0);
total.setText(String.valueOf(val6));
int val7 = myprefs.getInt("keyano", 0);
ano.setText(String.valueOf(val7));
}