..
Last method used to save permanent data that we will present in this guide are the Shared Prefereces. Special features of this methodology is to have a structure key / value easy to use.
For example you can set a string MyName for the key name. In a second step, you can retrieve the value associated with the key name simply looking inside the Shared Preferences.
This type of methodology for data storage in a premanente, is a middle ground between simple text files and more complex databases. The consideration made regarding the size of the set of data to be processed is identical to that made for the text file: in this case, if we have to handle very large data set, the Shared Preferences are not be the best choice.
A typical example that is made within the application is saving your preferences from the user, for example, the language used or the audio preferences.
We show now an example of using Shared Preferences in a sample application. In our code we are going to change the contents of a TextView depending on whether or not one was saved as. The sample code is as follows:
android.widget import .*;
android.content import .*;
extends Activity {public class provaSharedPreferences
Private LinearLayout layout;
private TextView tv;
/ ** Called When The activity is first created.
* /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
Button button = new Button (this);
layout = new LinearLayout (this);
layout.addView (button);
layout.addView (TV);
setContentView (layout);
SharedPreferences getSharedPreferences settings = ("TEST", 0);
String name = settings.getString ("name", "No value entered");
tv.setText ("result:" + name);
bottone.setOnClickListener (View.OnClickListener new () {
public void onClick (View view) {
SharedPreferences getSharedPreferences settings = ("TEST", 0);
SharedPreferences.Editor settings.edit editor = ();
editor.putString ("name", "Matthew");
editor.commit ();
String name = settings.getString ("name", "No value entered");
tv.setText ("result:" + name);
}
});
}
}
We are now going to make the usual analysis of the code. After declaring and creating widgets and layouts, we see that an object is declared SharedPreferences type that is initialized using the method getSharedPreferences. The latter returns to the Shared Preference given as the first parameter if it exists, otherwise creates and returns precisely this shared perference just created.
Then we go to retrieve the value associated with the key name using the getString method. The latter returns the value associated with the key referred to as the first parameter if the key exists, otherwise returns the value passed in as the second parameter.
Inside the listener associated with the button go back to retrieve the instance of the Shared Preferences and we wanted to create an object of type editors need to change the values within the Shared Preference for which it was created (this happens with settings.edit ()).
The updated value for the key name is executed by invoking the method on the object editor putString previously created. As a final step you make permanent changes just made by invoking the method commit and then we go inside the TextView to enter the new value for the key key.
| |
Excel (Ebook)
Create spreadsheets and calculation. Just 25 €. |
| |
Front Page (Ebook)
Create Web pages without knowing HTML. Just 25 €. |
| |
Javascript (Course)
Complete guide to client-side scripting. From 39 €. |