I'd like to know how to store information, such as this variable:
int t = 0;
Let's say that while using the application, the user did some operation that added + 5 to this variable.
How do I when the user closes the application, the value will remain 5 in the next use, instead of returning to 0?
I want to store the floats gf and mf in a database.
My code:
package com.gustavo.sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button send;
TextView say;
EditText num;
CheckBox g;
CheckBox m;
float gf = 0;
float mf = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bacon();
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String counter = num.getText().toString();
if (counter.equals("")) {
Toast.makeText(getApplicationContext(),
"Digite um valor numerico", Toast.LENGTH_SHORT)
.show();
} else {
float counterAsFloat = Float.parseFloat(counter);
if (g.isChecked() && m.isChecked()) {
Toast.makeText(getApplicationContext(),
"Selecione apenas um checkbox",
Toast.LENGTH_SHORT).show();
} else if (m.isChecked()) {
mf = mf + counterAsFloat;
say.setText("Math " + Float.toString(mf));
} else if (g.isChecked()) {
gf = gf + counterAsFloat;
say.setText("Geo " + Float.toString(gf));
} else
Toast.makeText(getApplicationContext(),
"Selecione um checkbox", Toast.LENGTH_SHORT)
.show();
}
}
});
}
private void bacon() {
g = (CheckBox) findViewById(R.id.checkBox1);
m = (CheckBox) findViewById(R.id.checkBox2);
send = (Button) findViewById(R.id.button1);
say = (TextView) findViewById(R.id.textView1);
num = (EditText) findViewById(R.id.editText1);
}
}