Misbehavior when trying to store a string set using SharedPreferences

I’m trying to store a set of strings using the SharedPreferences API.

Set<String> s = sharedPrefs.getStringSet("key", new HashSet<String>());

SharedPreferences.Editor editor = sharedPrefs.edit();

The first time I execute the code above, s is set to the default value (the just created end empty HashSet) and it is stored without problems.

The second and next times I execute this code, a s object is returned with the first element added. I can add the element, and during the program execution, it is apparently stored in the SharedPreferences, but when the program is killed, the SharedPreferences read again from its persistent storage and the newer values are lost.

How can the second, and elements after that, be stored so they won’t get lost?


This “problem” is documented on SharedPreferences.getStringSet.

The SharedPreferences.getStringSet returns a reference of the stored HashSet object inside the SharedPreferences. When you add elements to this object, they are added in fact inside the SharedPreferences.

That is ok, but the problem comes when you try to store it: Android compares the modified HashSet that you are trying to save using SharedPreferences.Editor.putStringSet with the current one stored on the SharedPreference, and both are the same object!!!

A possible solution is to make a copy of the Set<String> returned by the SharedPreferences object:

Set<String> s = new HashSet<String>(sharedPrefs.getStringSet("key", new HashSet<String>()));

That makes s a different object, and the strings added to s will not be added to the set stored inside the SharedPreferences.

Other workaround that will work is to use the same SharedPreferences.Editor transaction to store another simpler preference (like an integer or boolean), the only thing you need is to force that the stored value are different on each transaction (for example, you could store the string set size).


How do I pass custom data to Google Maps v2 InfoWindowAdapter?

I am trying to take advantage of an InfoWindowAdapter to provide custom content for the InfoView. I’m pulling down a JSONArray from my web service and adding the Markers but I’m not seeing how to pass the detail to the call back via a Marker.

    public View getInfoWindow(Marker marker) 

In the Javascript API I was able to just set arbitrary marker info. I want to be able to pass info that can be used as conditionals for the custom content, example a marker.status string. So something other than title etc. The view will need a number of custom fields I need to pass in.


perhaps try adding your info or object to the marker as explained here:

Edit: I also made a post that continues the previous to use the InfoWindowAdapter. Check it out here!


Attaching Java VisualVM for CPU-Profiling leads to JVMTI error 66

When using VisualVM for cpu-profiling of my application, I run into the error

“Profiler Agent Error: with message: Redefinition failed with error 66”.

Upon that, it copies all the class-files from the build path to the location I’ve started the application from.

I tried to find some information on it, the error code means (According to JVMTI-documentation):

A direct superclass is different for the new class version, or the set of directly implemented interfaces is different.

As profiling usually works in my environment (Java build 1.7.0_09-b05, Fedora 17 64-bit). I’ve tried “-Xshare:off” as said in the Visualvm-docs, without any effect.

As this is the only Java-SE application I’m experiencing this problem with, it seems that it is somehow related to the libraries I’m using. The following are in my classpath:

  • Log4j 1.2.17
  • Apache OpenJPA 2.2.1
  • Commons Configuration 1.9
  • Commons Pool 1.6
  • Postgresql 9.1-901jdbc4
  • imgscalr-lib-4.2

Can anyone see a correlation between the libraries and the error? I really don’t have an idea anymore.


It looks like this can be caused by JPA. You can use Sampler instead of Profiler. Another possibility is to define root methods and instrumentation filter so that classes instrumented by JPA are not profiled by VisualVM. See Profiling With VisualVM, Part 1 and Profiling With VisualVM, Part 2 to get more information about profiling and how to set profiling roots and instrumentation filter.


Design by contract and assert statements

I am interested in the Design by Contract approach. It seems that for preconditions checked exceptions must be used to enforce them.
But for post-conditions and class-invariants I think that assertions are preferred.
Am I right? If I am correct, why for post-conditions and class-invariants assertions which may be disabled are allowed? Shouldn’t post-conditions and invariants also be enforced?


Post conditions and class invariants on a component can only fail if the component itself is written incorrectly. Unit tests should catch all of these. It’s permissible, of course, to actually check them in production, but this isn’t necessarily worth the performance tradeoff.

On the other hand, preconditions can fail if the users of that component are incorrect. Tests on the component itself cannot check these, so it’s necessary to fail more actively so that those unit tests fail.


Master detail split styling in android

This question probably has been asked many times over here, without yielding useful answers. I’d place it here with a possible answer. Feel free to improve.

Question: How to setup popup like style of detail view, in side by side master detail view as highlighted in image below:

enter image description here


In this solution , I have used 9-patch background images, to define 2 kinds of backgrounds for list items. So, the selected(checked) list Item has a different background, as shown:

enter image description here (list item)

enter image description here (selected item)

enter image description here (list item)

The list item layout’s parent view, is a class extending LinearLayout (can be any ViewGroup) and implementing Checkable. Hence, when ListView is set to choice mode, it can automatically check/uncheck this view. This checked state is then used by selector background assigned to this view :

<selector xmlns:android="">
  <item android:state_checked="true" android:drawable="@drawable/checked"/>
  <item android:drawable="@drawable/unchecked"/>

This makes the background of list item view change automatically, without having to do it manually in code every time a list item is selected.


enter image description here

Additional points:

  1. Scrollbar can be moved to left side.
  2. Use list dividers that match shadow color, or more padding can be placed between list items and detail view.
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..