[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –
Android Application Development Tutorial – 97 – Loading SharedPreferences Data
by
Tags:
Comments
33 responses to “Android Application Development Tutorial – 97 – Loading SharedPreferences Data”
-
My app keeps crashing. Great explanations, but app doesn't work.
-
code of SharedPrefs class:
package com.utkarsh.thenewdelhi;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;public class SharedPrefs extends Activity implements OnClickListener {
EditText sharedData;
TextView dataResults;
Button save,load;
SharedPreferences someData;
public static String file="My Shared String"; // needed to use sharedpreferences class
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sharedpreferences);
setupVariables(); // user created method
someData=getSharedPreferences(file,0);// the string file is used here to get string data in 0 as private mode
}
private void setupVariables() {
// TODO Auto-generated method stub
save=(Button)findViewById(R.id.bSave);
load=(Button)findViewById(R.id.bLoad);
sharedData=(EditText)findViewById(R.id.etDataInput);
dataResults=(TextView)findViewById(R.id.tvDataRes);
save.setOnClickListener(this);
load.setOnClickListener(this);}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.bSave:
String stringData=sharedData.getText().toString(); // the string data taken here will go to file described above
// to edit text data using sharedpreferences editor
SharedPreferences.Editor editor=someData.edit(); // to edit the data taken above in somedata variable of type sharedpreferences
editor.putString("sharedString",stringData); // put edited string of editor in stringdata whith a key
editor.commit(); // editing done! now ok commit or save changes
break;
case R.id.bLoad:
someData=getSharedPreferences(file,0); //for check string updates
String dataReturn=someData.getString("sharedString", "couldn't load data(string)");// to get string from someData var of sharedpreferences
dataResults.setText(dataReturn);// show data in textview
break;
}
}}
-
Thank you MasterEmpire11 for the suggestion, I have been able to achieved the display using database and retrieved the
data entered via ListView. -
Hello just wondering if there is anyway to pass input data from my first activity and display the input
result in the fourth activity. (using putExtras, I was able to display result but it only display what I click on the third page. I tried shared Preferences too but data not found message keep showing up) Maybe ain't no good with the structure.
For instance;
page 1– gets users name (e.g popy)
page 2– shows a list of options to pick
page 3– shows information about the option. (on this option page is a button that shows other viewers.
Clicking the button takes you to the page where user's input is display)
page 4— shows/display present and previous viewers, including (popy) on the list.How can I
make it to display users-name rather than options
Any suggestion.
Thanks a lot.
-
Could you please share all the source code for this series? I checked the above mentioned forum but could not find the source code
-
lol sharded string
-
+losiram86 ..have the same issue. have you figured it out yet? How do i save more than one thing??
-
If you get an error, you probably have to import brain.
Review all and find mistake.
Man it,s a pain debugging with eclipse… -
thx. ur the best man
-
If I want to load 2 files I can't. It only shows the last one that I saved. why is that? how can I load all the 'folders' that I have saved?
-
toxicblu and Ian MacKinnon are great!
-
The Concept of your teaching n the tutorial is the best materials to start Android, n i thank you for that… But in previous n in this video you mention "static" variable as constant which cannot be changed. Up to my knowledge static means a variable which is common to all the objects of class n can be accessed using Class name or object but it can be changed. For constant we use "final" keyword. I hope you will correct it.
Thank again you for learning material of Android.
Sudip Lama -
What is your error?
-
I never mind, misinterpreted your comment.
-
I think you need the getSharedPref() just in case the first thing the user does when the app is loaded is 'Load' prefs rather than save first and then load.
-
It gives me an error, The New Boston stopped, but I put everything in Manifest and menu. What is the problem ?
-
shartedString
-
Oh thank you very much for the answer! 🙂
That's what I wanted to hear 🙂 -
Yes it keeps saved.
U just hadd to kill the app/reboot, open the app again and press at first load and see that the "old" save was still in memory -
But what's THE POINT of using SharedPreferences? Why couldn't we simply create a member variable "String getData;"
And then while saving say getData = sharedData.getText().toString();
and then while loading say dataResults.setText(getData);
In other words when do we want to use SharedPreferences? Is it when we turn off our app/phone but still want to preserve the input when we turn our phone/app on again? Or why exactly?
What's a practical example?Any help would be appreciated!!! 🙂
-
I think it should be public final String filename = …. if you don't want to change the value, not public static String….
-
O gosh the views have dropped to 10k. I am among the elite few!
-
i have an error on the line where it says setContentView(R.layout.sharedpreferences);
its only underlined in red under sharedpreferences and it says that it can not be resolved or is not in field. im pretty sure i have the same exact code. what should i do to get rid of this error? -
hey if you're still wondering, I think he changed the code on
dataResults = (TextView) findViewById(R.id.tvShowResults);
to dataResults = (TextView) findViewById(R.id.tvLoadSharedPrefs);
in the method setupVariables, atleast that's what did it for me -
pretty much answered my question.. thanks
-
any reason for writing someData = getSharedPreferences(filename,0); again at the load case of the switch statement… I guess not, if yes please share here
-
There is no need to getSharedPrefs again. See android docs: "Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made."
-
@MoaDjul & @supersushi269 Depends of the kind of object, but let's say you want to save an image. It would be better to save the image at a folder (use an output stream) and only save the route to that image at preferences to access it later.
That's why I don't think it's necessary to save any other kind of objects -
@MoaDjul Saving more complex objects like images would be wonderful but it seems like we can save only primitives and String 🙁
-
@ul1ss369 Check your dataResults TextView is setup properly in the setupVariables() method, I had the same problem and discovered that I had it set up to a tvResults which was in another activity instead of what it should have been tvLoadSharedPrefs.
-
Why when I click on Load my app crash?
-
i see blood instead of bLoad
-
so is the mySharedString that is static just the name of the so called folder?
Leave a Reply