I want to take the value that I have in the TextView
, add it, save it and repeat this process every time the button is touched, but I don't understand why I can't save it.
My code:
public class MainActivity extends Activity {
Button b1;
TextView tv3,tv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.b1);
tv3=(TextView)findViewById(R.id.tv3);
tv4=(TextView)findViewById(R.id.tv4);
SharedPreferences preferences=getSharedPreferences("Saldo", Context.MODE_PRIVATE);
tv4.setText(preferences.getString("Valor", ""));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void a(View view){
int x=200;
String valor1=tv4.getText().toString();
int nro1=Integer.parseInt(valor1);
int sumar=nro1 + x;
String resultado=String.valueOf(sumar);
tv4.setText(resultado);
SharedPreferences preferences=getSharedPreferences("Saldo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
editor.putString("Valor", tv4.getText().toString());
editor.commit();
}
}
You have some small bugs in the code, but don't worry, I'll try to explain them to you as best as possible:
It turns out that if you want the action of adding and saving to happen when the button is pressed, you are doing it wrong, since for a button to be linked with the action, it can be done in several ways:
NOTE: if you already know the ways to link an action with a button, go to the section of the
SharedPreferences
, just below the explanation of the buttons.BIND A METHOD TO A BUTTON
In this way you have to create a method in your code
Java
and from the properties of the button in the sectionXML
,Desing
you would have to look for a property calledonClick
and assign the method you have created there. It is not necessary to pass anyView
to this method as you do in the methoda()
IMPLEMENT THE ONCLICKLISTENER METHOD
To do this, you have two ways:
In your main class declaration you add the following and implement the necessary codes:
The other way is by implementing it directly to your button in the method
onCreate()
:Once you know this, I will explain
SharedPreferences
and you will understand why it fails you:SHARED PREFERENCES
To get a value from the
SharedPreferences
use this code:SharedPreferences preferences = getSharedPreferences("Balance", Context.MODE_PRIVATE); textview.setText(preferences.getString("Value", "5"));
The first line declares the file
SharedPreferences
to be accessed. In this case it will be calledSaldo
. When this line is executed, a file called will be created on your Android deviceSaldo
that will be accessed whenever you declare it in the first line, no matter whatActivity
(screen) you are on.In the second line you are saying to add a value to the
TextView
one that is stored with the key nameValor
and that in case the keyValor
does not exist, it will be created and its initial value will be5
.In your app, you have put
""
, so you will NEVER get anything or add anything and it does execute the action, but the value is still""
, that is, nothing.In case you want to save, the following code is executed, as you have in your code:
Try to use
apply()
to save the data instead ofcommit()
.Now you know the reason for your error and I leave you an example code that I have made:
MainActivity.java
activity_main.xml
:I hope everything is clear to you, but even so, if you need anything or you just don't understand something I just explained to you, write me a comment and I'll solve your doubt.
Good luck with your app!
Try to use a single instance of
SharedPreferences
, declare a global instance in your class as well as the editor, and use that same instance to fetch the information and to store it, with this you ensure that you are using the same register ofSharedPreferences
, sometimes even if you put the tag in your case 'Saldo', being another instance of shared preferences, the values are not the same.The reason is that you are calling the method from the button in the layout, defining execute the method
a(View view)
You don't have the method call defined:
add the property android:onClick="a" :
to call the method
a()
from the Layout.If you were to call the same method by calling the method via a
OnClickListener
in code, the method would change to: