How to save dynamically added items in Listview with sharedpreferences?

前端 未结 3 1159
青春惊慌失措
青春惊慌失措 2021-01-27 08:14
        protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.a         


        
相关标签:
3条回答
  • 2021-01-27 08:31

    You can store string sets in Shared Preferences, then convert them back to a List when you retrieve them.

    ArrayList<String> list = new ArrayList<String>();
    list.add("test1");
    list.add("test2");
    
    PreferenceManager.getDefaultSharedPreferences(context)
        .edit()
        .putStringSet("stringset", new HashSet<String>(list))
        .commit();
    
    0 讨论(0)
  • 2021-01-27 08:35

    You are supposed to store single key-value paired data with SharedPreferences. Trying to store big grouped data is not efficient at all. You should use an SQLite database.

    SQLite and ContentProvider Tutorial

    0 讨论(0)
  • 2021-01-27 08:41

    You can save to shared preference in this way

    SharedPreferences sp = context.getSharedPreferences(
                "file name", 0);
        SharedPreferences.Editor spEdit = sp.edit();
        spEdit.putLong(key, value);
        spEdit.commit();
    
    0 讨论(0)
提交回复
热议问题