How can I store a HashMap in android using shared preferences?

后端 未结 3 1305
渐次进展
渐次进展 2020-12-18 12:56

I have created a HashMap in the following way:

HashMap buttons = new HashMap();

I need this to rem

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 13:31

    Hey I found a way in the end :)

    I just changed the HashMap I had to format and then did the following to save the contents:

    SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, 0).edit();
    for( Entry entry : backUpCurency_values.entrySet() ) 
    editor.putString( entry.getKey(), entry.getValue() );
    editor.commit();
    

    and the following to retrieve the HashpMap:

    SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
    for( Entry entry : prefs.getAll().entrySet() )
       backUpCurency_values.put( entry.getKey(), entry.getValue().toString() );
    

提交回复
热议问题