0

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.

1 Answers1

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