如何从其他活动中读取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>
答案 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());
希望这会有所帮助〜