I have one list of cities in listview.On click of list item i want to store this value in sharedpreferences and get in new activity.In this activity want to stare this values in array. I want to make a world Clock given in every devices.
Asked
Active
Viewed 482 times
0
-
what you have tried? – ρяσѕρєя K Feb 02 '15 at 18:23
-
1Use intent.putextra() to pass the values from activity to another. Do not use shared preference in this case. – Vilas Feb 02 '15 at 18:23
-
This answer will help you. **http://stackoverflow.com/questions/3848148/sending-arrays-with-intent-putextra** – GIGAMOLE Feb 02 '15 at 18:34
1 Answers
0
The nice thing about using SharedPreferences
is that they are globally accessible within the context of your application - you do not need to "pass" them around. Furthermore, you can handle this nicely with the putStringSet
method of SharedPreferences
. In your onClick
method, simply save your cities as follows:
Set<String> cityList = new HashSet<String>();
cities.add("City1");
cities.add("City2");
// Repeat for all cities in your list...
SharedPreferences sharedPreferences = getSharedPreferences("yourPreferences", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences .edit();
editor.putStringSet("cityList", cityList);
editor.commit();
Then, when you wish to retrieve the cities in your new Activity
, simply use the following code:
SharedPreferences sharedPreferences = getSharedPreferences("yourPreferences", Activity.MODE_PRIVATE);
Set<String> cityList = sharedPreferences.getInt("cityList", new Set<String>());

Willis
- 5,308
- 3
- 32
- 61