2
I had to watch a tuturial on youtube and copied this code to save and delete entered values, my doubt is that always appears that "0" as below and I would like to know how I can takeit without error, I also leave the code and it is clear if there is another easier way to use the 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));
}
Would you like null instead of 0? If that is so, since you want to save the values as text and do not do any operation on the numbers as int, you could directly take the preference value as String:
banca.setText(myprefs.getString("keybanca",null));
. The second argument of getString() (or getInt()) is the default value for that property, that is, the value returned by the method if it has not been defined.– mari
My goal is to insert only numbers and save inside Edittext, but the way I have the code in Edittext running the application always appears "0" and I did not want. I could edit my code that way. Thank you
– S0nkit3