gpt4 book ai didi

java - 使用 SharedPreferences 在 Android 中实现 Collection 夹列表

转载 作者:行者123 更新时间:2023-11-30 05:25:23 27 4
gpt4 key购买 nike

我想使用 SharedPreferences 创建 Collection 夹列表我正在尝试在其中保存一些列表SharedPreferences 中有一个用于保存字符串集的选项。但不会保留重复值。我想用 SharedPreferences 存储重复项列表。我应该怎么办?

另外我如何转换 ArrayList<String>HashSet<String>

感谢您的回答!

最佳答案

  1. java 将 list 转换为 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);
  • 我建议使用intentbundle来存储列表
  • Activity 到 Activity

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

    存储数据:

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

    检索数据:

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

    Activity fragment

    Android passing ArrayList<Model> to Fragment from Activity

    存储数据:

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

    检索数据:

    // fragment a
    Bundle extras = getIntent().getExtras();
    List<String> arraylist = extras.getParcelableArrayList("test");

    关于java - 使用 SharedPreferences 在 Android 中实现 Collection 夹列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58768645/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com