1

I want to create a favorite list using SharedPreferences and I am trying to save some lists in it. There is an option for saving a String Set in SharedPreferences but the set does not keep Duplicate values. I want to store a list of duplicates with SharedPreferences. What should I do?

Also how can I convert an ArrayList<String> to HashSet<String> ?

Thanks for your answers!

  • convert `list` to `set` : https://www.mkyong.com/java/how-to-convert-list-to-set-arraylist-to-hastset/ – Donald Wu Nov 08 '19 at 14:47
  • also if `SharedPreferences` cannot store `list`..try another way to store your variable..is it an activity to fragment..or fragment to activity? – Donald Wu Nov 08 '19 at 14:48
  • i suggest u to use `intent` to store the variable..then when u change the page..u can get it from `intent` – Donald Wu Nov 08 '19 at 14:49

1 Answers1

1
  1. java convert list to set:

http://mkyong.com/java/how-to-convert-list-to-set-arraylist-to-hastset

List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");

Set<String> set = new HashSet<String>(list);
  1. I suggest using intent and bundle to store the list

Activity to Activity

Pass list of objects from one activity to other activity in android

Store data:

// activity a
Intent intent = new Intent(getApplicationContext(),YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("test", arrayList);
intent.putExtras(bundle);
startActivity(intent);

Retrieve data:

// activity b
Bundle bundle = getIntent().getExtras();
Object test = bundle.getParcelable("test");

Activity to Fragment

Android passing ArrayList<Model> to Fragment from Activity

Store data:

// activity a
Bundle bundle = new Bundle();  
bundle.putParcelableArrayList("test", arraylist);
fragment.setArguments(bundle);

Retrieve data:

// fragment a
Bundle extras = getIntent().getExtras();  
List<String> arraylist = extras.getParcelableArrayList("test");
Donald Wu
  • 698
  • 7
  • 20