简体   繁体   English

如何保存HashMap <String, Parcelable> 归档?

[英]How to save HashMap<String, Parcelable> to file?

Sorry for my poor English. 对不起,我的英语不好。

I have a HashMap<String, Parcelable> to save some InstanceState s of a RecyclerView ,and I want to save the HashMap<String, Parcelable> into a file . 我有一个HashMap<String, Parcelable>保存一个RecyclerView InstanceState ,我想将HashMap<String, Parcelable>保存到一个文件中

I use the ObjectOutputStream ,it can write into a file,but when I use ObjectInputStream ,it seems that it doesn't have read some useful data,because the state of the RecyclerView doesn't change to the state that has been saved. 我使用ObjectOutputStream ,它可以写入文件,但是当我使用ObjectInputStream ,它似乎没有读取一些有用的数据,因为RecyclerView的状态不会更改为已保存的状态。

Why this doesn't work? 为什么这不起作用?

How can I save and restore InstanceState of view by "key-value" ??? 如何通过“键值”保存和恢复视图的InstanceState

I want to save InstanceState permanently . 我想永久保存InstanceState

Thank you for your help~ 谢谢你的帮助〜

my code: 我的代码:

private HashMap <String, Parcelable> hmRecyclerViewState = new HashMap<String, Parcelable>();    
private Parcelable recyclerViewState;
ObjectOutputStream oos;
ObjectInputStream ois;

1. 1。

    try {
        ois = new ObjectInputStream(new FileInputStream(stateFile));
        hmRecyclerViewState = (HashMap<String, Parcelable>)ois.readObject();
    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (OptionalDataException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {  
        if (ois != null) {  
            try {  
                ois.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }
    }

2. 2。

    try {
        oos = new ObjectOutputStream(new FileOutputStream(stateFile));
        if (hmRecyclerViewState != null)
            oos.writeObject(hmRecyclerViewState);
        oos.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {  
        try {  
            if (oos != null)  
                oos.close();  
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }  
    }  

3. 3。

public void saveRecyclerViewState(String tableName) {
    recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
    hmRecyclerViewState.put(tableName, recyclerViewState);
}

4. 4。

public void restoreRecyclerViewState(String tableName) {
    if ( hmRecyclerViewState.get(tableName) != null) {
        recyclerViewState = hmRecyclerViewState.get(tableName);
        recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
        recyclerViewState = null;
    }
}

You can save Parcelable objects to the String or XML or JSON type. 您可以将Parcelable对象保存为StringXMLJSON类型。 And then you can save them to files. 然后您可以将它们保存到文件中。 By the way, the HashMap Object implements the Serializable, so you can save HashMap Object to file without any problems. 顺便说一下,HashMap对象实现了Serializable,因此您可以将HashMap对象保存到文件中而不会出现任何问题。

There are some informations about PARCELABLE VS. 有一些有关PARCELABLE VS的信息 JAVA SERIALIZATION IN ANDROID APP DEVELOPMENT Java应用开发中的JAVA序列化

If i followed your question than you can do something like.. Iterate over your map >> put into a String builder >> save it into file. 如果我遵循您的问题,则可以执行以下操作:遍历您的地图>>放入“字符串”构建器>>将其保存到文件中。 In your case create a toString() method in your parcelable class that returns all fields like(id,name,address...) in a format like comma separated. 在您的情况下,请在可打包类中创建toString()方法,该方法以逗号分隔的格式返回所有字段,例如(id,name,address ...)。

StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Parcelable> entry : map.entrySet()) {
    String key = entry.getKey();
    Parcelable value = entry.getValue();
   // append it to String builder
    sb.append(key+":"value.toString()+"\n");
}
// write here to a file..

and when you want to restore read from file >> put it into Map >> restore it. 当您想还原文件>>并将其放入Map >>还原时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM