I have list of Installed apps, When i click any of them i can get the package name of clicked App, Now I want to Save the clicked package name in an ArrayList of String to SharedPreferences with Gson. and fetch the saved package name.
I checked some of previous answers but i didn't get the solution i wanted.
This is the code which i want to save the package name of clicked app.
String ApplicationPackageName = adapter.stringList.get(position);
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(ApplicationPackageName);
editor.putString("task list", json);
editor.apply();
And This is the Code which i want to fetch the saved package name.
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
Gson gson = new Gson();
String json = sharedPreferences.getString("task list", null);
Type type = new TypeToken<ArrayList<String>>() {}.getType();
arrayList = gson.fromJson(json, type);
if (arrayList == null) {
arrayList = new ArrayList<>();
}
But I'm getting this error. Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 2 path $
Any Help ?