kullaniciati kullaniciati - 2 months ago 7
Android Question

How to use getStringSet() method in SharedPreferences?

I have two different activities in my app. First activity contains 3 fragments, which has buttons in order to add a string to SharedPreferences and the second activity is used to display all strings that are added. Since I add more than one string, I figured out that I have to use

getStringSet()
method. I don't have any error in my fragment code but it crashes in debug mode due to
ClassCastException
. According to stack the problem is because of
getStringSet
. Stack message is:


"SharedPreferencesImpl.getStringSet(String, Set) line: 234")


This is the code in fragment:

public class FirstFragment extends Fragment {

public static final String filename = "imagine_dragons";
SharedPreferences someData;
Button bMer;
String merStr = "string from first fragment";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_first, container,
false);

bMer = (Button) rootView.findViewById(R.id.bMer);
bMer.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

someData = getActivity().getSharedPreferences(filename,0);
SharedPreferences.Editor editor = someData.edit();
Set<String> sharedString = someData.getStringSet("sharedString", new HashSet<String>());
sharedString.add("merStr");
editor.putStringSet("sharedString", sharedString);
editor.commit();
}
});
return rootView;
}
}


This is the code for second activity. Here comes another challenge about setText. Since we use string set in SharedPreference, we can't use setText method to display the string set. Therefore this code contains error(EDITED):

public class SecAct extends Activity {
public static final String filename = "imagine_dragons";
SharedPreferences someData;
TextView tvSec;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sec);
tvSec = (TextView) findViewById(R.id.tvSec);
someData = getSharedPreferences(filename, 0);
Set<String> sharedString=someData.getStringSet("sharedString", new HashSet<String>());
if(sharedString != null){

Iterator<String> iterator = sharedString.iterator();

while(iterator.hasNext()){

String id = iterator.next();

int start = id.indexOf("[") + 1;
int end = id.indexOf("]")-1;

String items = String.copyValueOf(id.toCharArray(), start, end);


}
}
}
}


My minSDK value is 14 so it supports thie getStringSet() method.

Here is the LogCat:

01-02 16:02:39.727: W/asset(7824): Copying FileAsset 0x57249048 (zip:/data/app/com.example.ikincitaslak-1.apk:/resources.arsc) to buffer size 9272 to make it aligned.
01-02 16:02:39.732: W/ActivityThread(7824): Application com.example.ikincitaslak is waiting for the debugger on port 8100...
01-02 16:02:43.512: E/(7824): file /data/data/com.nvidia.NvCPLSvc/files/driverlist.txt: not found!
01-02 16:02:49.827: W/dalvikvm(7824): threadid=1: thread exiting with uncaught exception (group=0x40f33a08)
01-02 16:02:49.847: E/AndroidRuntime(7824): FATAL EXCEPTION: main
01-02 16:02:49.847: E/AndroidRuntime(7824): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ikincitaslak/com.example.ikincitaslak.Sepet}: java.lang.StringIndexOutOfBoundsException: length=9; regionStart=0; regionLength=-2
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2463)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.os.Looper.loop(Looper.java:158)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread.main(ActivityThread.java:5751)
01-02 16:02:49.847: E/AndroidRuntime(7824): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 16:02:49.847: E/AndroidRuntime(7824): at java.lang.reflect.Method.invoke(Method.java:511)
01-02 16:02:49.847: E/AndroidRuntime(7824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
01-02 16:02:49.847: E/AndroidRuntime(7824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
01-02 16:02:49.847: E/AndroidRuntime(7824): at dalvik.system.NativeStart.main(Native Method)
01-02 16:02:49.847: E/AndroidRuntime(7824): Caused by: java.lang.StringIndexOutOfBoundsException: length=9; regionStart=0; regionLength=-2
01-02 16:02:49.847: E/AndroidRuntime(7824): at java.lang.String.failedBoundsCheck(String.java:587)
01-02 16:02:49.847: E/AndroidRuntime(7824): at java.lang.String.<init>(String.java:419)
01-02 16:02:49.847: E/AndroidRuntime(7824): at java.lang.String.copyValueOf(String.java:716)
01-02 16:02:49.847: E/AndroidRuntime(7824): at com.example.ikincitaslak.Sepet.onCreate(Sepet.java:36)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.Activity.performCreate(Activity.java:5165)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
01-02 16:02:49.847: E/AndroidRuntime(7824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2419)
01-02 16:02:49.847: E/AndroidRuntime(7824): ... 11 more

Answer

TextView does not have a setText method that takes a Set<Strings> as a parameter. If you want all the Strings in the set to be displayed in the TextView, you will have to create a single String that is a conjunction of the set.

There are plenty of tutorials on how to do such a thing: one such method