I would like to know how(if possible) to send my String from my Shared preferences, to an array of Strings I have so that my other class will be able to read it so the images can be displayed.
I know that getting "imgUrl" will give me the correct image URL corresponding to the image that is being viewed.
I'm really stuck on how I would be able to achieve this, any help would be great..
Class:
case R.id.FavouriteWallpaper:
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("imgUrl", mImageUrl);
editor.commit();
break;
}
return super.onOptionsItemSelected(item);
}
//Somehow get "imgUrl" from Sharedprefs to be displayed in this format:
public static final String[] ImageFavs = new String[] {
"www.URLTOBEDISPLAYEDHERE.jpg"
};
Have you tried this.?
String uri=prefs.getString("imgUrl", "default uri");
Related
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Orderinfo", responseStr);
Log.i("myprev",responseStr);//storing correct values
editor.commit();
I have used fragments,i stored Orderinfo,its stores the current values but in next fragment page i have to retrive my current customer data,but its shows previously stored customer details...can anyone help me to solve the problem?
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
String emailsts = prefs.getString("Orderinfo", "");
Log.i("myinfo",emailsts);//this shows prev customer details
SharedPreferences myPrefs =PreferenceManager.getDefaultSharedPreferences(getActivity());
myPrefs.edit().putString("Orderinfo", responseStr).apply();
String responseString= myPrefs.getString("Orderinfo", "");
if(!responseString.isEmpty()){
// Do you code
}
String emailsts = prefs.getString("Orderinfo", "");
here "emailsts" is getting null so you will get default value which is blank in above code .which means your value is not Store in SharedPreferences.
And Mau be your "responseStr" is null please put log there and print value.
Log.v(TAG ,"responseStr=="+responseStr);
i working sharedPreferences.i wrote code witch can to save information(some strings) and also can show this information in another fragment.now i want to recieve like this result.for example first time i saved 10 and then 20.in a another fragment i can show only 20,but i want to show sum this (10+20).meybe this question is easy but i do not know how i can do this
this is my source
private static String MY_PREFS = "mysessio";
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
sharedPreferences = getActivity().getSharedPreferences(MY_PREFS, 0);
editor = sharedPreferences.edit();
_price_counter_int = Integer.parseInt(price_counter
.getText().toString().trim());
_price = _price * _price_counter_int;
editor.putString("price",
String.valueOf(_price));
editor.commit();
and another fragment code
String MY_PREFS = "mysessio";
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
String price_result;
sharedPreferences = getActivity().getSharedPreferences(MY_PREFS, 0);
editor = sharedPreferences.edit();
price_result = sharedPreferences.getString("price", "");
int ab1 = Integer.parseInt(price_result);
Toast.makeText(getActivity(), String.valueOf(ab1), Toast.LENGTH_SHORT)
.show();
I would suggest to do something like this:
1. Before writing 20, get the value 10
2. Concatenate 20 with 10 with some character (Example 10:20)
3. At the time of getting sum, get the entire string (10:20)
4. Split the string with splitter character :
5. Get the sum
This will allow you to have access to all the consecutive additions to the SharedPreferences.
In case your only concern is to store the sum, you can directly get the value 10, add 20 to it and commit the sum to SharedPreferences.
sharedPreferences = getActivity().getSharedPreferences(MY_PREFS, 0);
editor = sharedPreferences.edit();
_price_counter_int = Integer.parseInt(price_counter.getText().toString().trim());
_price1 = _price * _price_counter_int;
editor.putString("price",String.valueOf(_price));
editor.putString("price1",String.valueOf(_price1));
editor.commit();
Then you can retrieve both the values and display them.
My application is basically a quiz that presents people with world flags. I want to add a save function that adds the current flag to a separate list. Right now, this is what I do when they click the "save" button:
saveListGC.add(playList.get(1));
(playList.get(1) is the current flag). The problem is, I have to re-define the saveListGC every time the script starts, and that empties the contents:
public static ArrayList<String> saveListGC = new ArrayList<String>();
So what I'm wondering is how can I save this data, and re-load it later? I've seen things about using SharedPrefernces, but I don't really understand it. If someone could please explain it as easily as possible, I would really appreciate it. Thank you!
This a simple example of how you can use SharedPreferences:
//intiat your shared pref
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
//store data to pref
editor.putString("myString", "value 1");
editor.commit();
//retrieve data from pref
String myString = pref.getString("myString", null);
But the real problem here is that SharedPreferences can not store an Object of type List (ArrayList), but you can store an Object of type Set (like Hashset) using this method
Set<String> mySet = new HashSet<String>();
mySet.add("value1");
mySet.add("value2");
mySet.add("value3");
editor.putStringSet("MySet", mySet);
So to answer your second question, this what i propose for you to do:
//This is your List
ArrayList<String> saveListGC = new ArrayList<String>();
//Convert your List to a Set
Set<String> saveSetGC = new HashSet<String>(saveListGC);
//Now Store Data to the SharedPreferences
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putStringSet("MySet", saveSetGC);
editor.commit();
//After that in another place or in another Activity , Or every where in your app you can Retreive your List back
pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Set<String> mySetBack = pref.getStringSet("MySet", null);
//Convert Your Set to List again
ArrayList<String> myListBack = new ArrayList<String>(mySetBack);
//Here you can se the List as you like...............
Log.i("MyTag", myListBack.get(0));
Log.i("MyTag", myListBack.get(1));
Good Luck :)
I would like to know how I would be able to able achieve getting the strings from my shared preferences(I have already set up), then outputting it to a String array so that my Image Fetcher will be able to read it(It has to be an array for it to read).
In one activity I am setting the shared preferences:
case R.id.FavouriteWallpaper:
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("imgUrl", mImageUrl);
editor.commit();
}
return super.onOptionsItemSelected(item);
}
In another class I am getting the string from shared preferences:
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
prefs.getString("imgUrl", null); //Output imgUrl to String Array somehow
// I would like my image fetcher to read a string array that has been fetched from shared preferences.
mImageFetcher.loadImage(Fragment3.imgUrl[position
- mNumColumns], imageView);
return imageView;
}
You can not really save an array to SharedPrerences, but you can store all your urls in a string like CSV (comma separated values) and store it in sharePreferences. You can write a manager that will have 2 methods. One to transform your array to CSV and store and the second get the CSV and transform it to an array.
The second method is to save the data like a JSONArray to do this you also need to write a manager that will have 2 methods also, one to transform your array to JSONArray and store it in Shared Preferences, and the second to transform the jsonArray into your array.
you can get the strings using for loop
examole...
SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = prefs.edit();
editor.putString("imgUrl", mImageUrl);
for(int i=0; i<arraylist.size(); i++)
{
editor.putString("imgUrl"+i, arraylist.get(i).toString());
}
editor.commit();
I am using Android 2.1 sdk and I am trying to save user loggin session in to Shared preferences, the thing is after saving the value to the shared preference I am unable to retrive it. Here I am pasting the code I used to save and fetch value from SharedPrefrence.
public void setValue(String name, String value, String prefName) {
sharedPref = mContext.getSharedPreferences(prefName, Context.MODE_PRIVATE);
sharedPref.edit().putString(name, value);
sharedPref.edit().commit();
}
public String getValue(String name, String prefName) {
String value = null;
sharedPref = mContext.getSharedPreferences(prefName, Context.MODE_PRIVATE);
value = sharedPref.getString(name, value);
return value;
}
Did i miss some thing in this code, I am not retrieving any exceptions while saving and retrieving the value. Thanks for any help.
Every call to edit() returns you a new Editor instance. So you get an instance, make a change and leave it alone. Then you get a second one and commit that without changes, which results in no value changes in the preferences.
Rather chain in the commit():
sharedPref.edit().putString(name, value).commit();
Alternatively break it up into multiple lines with one specific instance:
Editor e = sharedPref.edit();
e.putString(name, value);
e.commit();
private SharedPreferences myPrefs;
myPrefs = Actionactivity.this.getSharedPreferences("myPrefs", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("Mobile_no", getText_no.getText().toString().trim());
prefsEditor.commit();
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
myPrefs.getString("Mobile_no", "");
try this one code work