如何将对象的hashmap保存到共享首选项

时间:2015-06-23 09:30:32

标签: android hashmap sharedpreferences

我有一个对象 hashmap 。我想将其保存到共享 首选项并获取它。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

请检查我添加了MainActivity.java文件的完整代码,如下所示:

public class MainActivity extends ListActivity {

HashMap<String, Rajdata> map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    map = new HashMap<String, Rajdata>();
    for (int i = 0; i < 10; i++) {

        Rajdata raj = new Rajdata();
        raj.setNamString("My Data" + i);
        map.put("MyData" + i, raj);
    }

    saveData(MainActivity.this, map);
    // Assign adapter to List

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void saveData(Context context, HashMap<String, Rajdata> mlist) {
    SharedPreferences shared;
    SharedPreferences.Editor editor;

    shared = context.getSharedPreferences("MONDAY", Context.MODE_PRIVATE);
    editor = shared.edit();

    Gson gson = new Gson();
    String json = gson.toJson(mlist);
    editor.putString("mondAy", json);
    editor.commit();
    Fetchdata();
    // String getSaved = shared.getString("mondAy", null);
    /*
     * Toast.makeText(MainActivity.this, getSaved,
     * Toast.LENGTH_LONG).show();
     */

}

public void Fetchdata() {
    SharedPreferences shared;
    SharedPreferences.Editor editor;
    Gson gson = new Gson();
    shared = getSharedPreferences("MONDAY", Context.MODE_PRIVATE);
    editor = shared.edit();
    HashMap<String, Rajdata> listDayItems = gson.fromJson(
            shared.getString("mondAy", null),
            new TypeToken<HashMap<String, Rajdata>>() {
            }.getType());
    printMap(listDayItems);

}

public static void printMap(HashMap<String, Rajdata> mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry) it.next();
        System.out.println(pair.getKey() + " = "
                + ((Rajdata) pair.getValue()).getNamString().toString());
        it.remove(); // avoids a ConcurrentModificationException
    }
}
}

Custom Rajdata课程如下:

public class Rajdata {

String namString;

public String getNamString() {
    return namString;
}

public void setNamString(String namString) {
    this.namString = namString;
}

}

答案 1 :(得分:-2)

请做这样的事情 -

//variable for Shared preference.
        private SharedPreferences _sPrefs;
        private Editor _editorSpref = null;



        _sPrefs =getSharedPreferences("MYPREF", Context.MODE_PRIVATE);
        _editorSpref =_sPrefs.edit();


    // fetch data from HashMap
        HashMap<String, String> pairs = new HashMap<String, String>();
        Iterator myVeryOwnIterator = pairs.keySet().iterator();
        while(myVeryOwnIterator.hasNext()) {
            String key=(String)myVeryOwnIterator.next();
            String value=(String)pairs.get(key);
            _editorSpref.putString(key,value);
            _editorSpref.commit();
        }