如何从共享首选项中检索数组列表并将数组列表设置为recycleler视图

时间:2017-05-19 13:56:06

标签: android arrays list android-recyclerview sharedpreferences

如何从其他活动中读取SharedPreferences值?

如何阅读其他活动的偏好?

<?xml version="1.0" encoding="utf-16"?>
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
<asx:values>
<VBAK>
<VBELN>13</VBELN>
<VBAPS>
    <item_VBAP>
        <MATNR>C20050</MATNR>
        <VBPAS>
            <item_VBPA>
                <PARVW>AG</PARVW>
            </item_VBPA>
        </VBPAS>
    </item_VBAP>
</VBAPS>
</VBAK>
</asx:values>
</asx:abap>                   

1 个答案:

答案 0 :(得分:0)

使用HashSet将字符串ArrayList存储到SharedPreferences

以下是一个例子:

#。messages存储到SharedPreferences

public static final String KEY_MESSAGES = "messages";
public static final String SHARED_PREF_NAME = "mypref";

............
.................
public boolean saveKeyMessage(String message) {

    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    // Get existing messages
    Set<String> messages = getMessages();

    // Add new message to existing messages
    messages.add(message);

    // Store messages to SharedPreferences
    editor.putStringSet(KEY_MESSAGES, messages);
    editor.apply();

    return true;
}

#。messages获取SharedPreferences

public Set<String> getMessages() {

    SharedPreferences sharedPreferences = mCtx.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);

    // Get messages
    Set<String> messages = sharedPreferences.getStringSet(KEY_MESSAGES, new HashSet<String>());

    return messages;
}

#。ArrayList获取SharedPreferences

ArrayList<String> messageList = new ArrayList<String>(getMessages());

希望这会有所帮助〜