保存Bundle对象到共享preference [英] Saving Bundle object into shared preference
本文介绍了保存Bundle对象到共享preference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个活动
活动1
活性2
活动1:
意向意图=新意图(Activity1.this,Acivity2.class);
束B =新包();
b.putParcelableArrayList(actionArray,(ArrayList的&所述;?延伸Parcelable&GT)AllListArray.get(0));
b.putParcelableArrayList(comedyArray,(ArrayList的&所述;?延伸Parcelable&GT)AllListArray.get(1));
b.putParcelableArrayList(loveArray,(ArrayList的&所述;?延伸Parcelable&GT)AllListArray.get(2));
b.putParcelableArrayList(classicsArray,(ArrayList的&所述;?延伸Parcelable&GT)AllListArray.get(3));
b.putParcelableArrayList(familyArray,(ArrayList的&所述;?延伸Parcelable&GT)AllListArray.get(4));intent.putExtras(二);
startActivity(意向);
活性2:
活性2:
公开名单<电影及GT; actionArray;
公开名单<电影及GT; comedyArray;
公开名单<电影及GT; loveArray;
公开名单<电影及GT; classicsArray;
公开名单<电影及GT; familyArray;
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
捆绑recdData = getIntent()getExtras()。
如果(recdData!= NULL){
actionArray = recdData.getParcelableArrayList(actionArray);
comedyArray = recdData.getParcelableArrayList(comedyArray);
loveArray = recdData.getParcelableArrayList(loveArray);
classicsArray = recdData.getParcelableArrayList(classicsArray);
familyArray = recdData.getParcelableArrayList(familyArray);
}
}
我的要求是存储捆绑(recdData)
到共享preferences
的活性2?
可以在任何一个可以帮助我?
解决方案
preferences = mActivity.getShared preferences(
分享preference_demo,Context.MODE_PRIVATE); 地图<字符串,字符串> demoarraylist =新的HashMap<字符串,字符串>();
demoarraylist.put(actionArray,行动);
demoarraylist.put(comedyArray,喜剧);
demoarraylist.put(lovearray,爱);
demoarraylist.put(classicArray,经典);
demoarraylist.put(familyArray,家庭);共享preferences.Editor编辑器= preferences.edit();
对于(进入<字符串,字符串>项:demoarraylist.entrySet()){
editor.putString(entry.getKey(),entry.getValue());
}
editor.commit();
把上面的code把你的第一个活动。
和低于code是把你的第二次活动。
地图<字符串,字符串> demoarraylist =新的HashMap<字符串,字符串>();
共享preferences preferences = activity.getShared preferences(
分享preference_demo,Context.MODE_PRIVATE);
对于(进入<弦乐,>条目:?preferences.getAll()的entrySet()){
demoarraylist.put(entry.getKey(),entry.getValue()的toString());
}
。迭代myVeryOwnIterator = demoarraylist.keySet()迭代();
而(myVeryOwnIterator.hasNext()){
字符串键=(字符串)myVeryOwnIterator.next();
字符串值=(字符串)MapListUserStatus.get(密钥);
}
I have 2 Activities
Activity1
Activity2
Activity1:
Intent intent= new Intent(Activity1.this,Acivity2.class);
Bundle b=new Bundle();
b.putParcelableArrayList("actionArray", (ArrayList<? extends Parcelable>) AllListArray.get(0));
b.putParcelableArrayList("comedyArray", (ArrayList<? extends Parcelable>) AllListArray.get(1));
b.putParcelableArrayList("loveArray", (ArrayList<? extends Parcelable>) AllListArray.get(2));
b.putParcelableArrayList("classicsArray", (ArrayList<? extends Parcelable>) AllListArray.get(3));
b.putParcelableArrayList("familyArray", (ArrayList<? extends Parcelable>) AllListArray.get(4));
intent.putExtras(b);
startActivity(intent);
Activity2:
Activity2:
public List<Movie> actionArray;
public List<Movie> comedyArray;
public List<Movie> loveArray;
public List<Movie> classicsArray;
public List<Movie> familyArray;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle recdData = getIntent().getExtras();
if(recdData!=null) {
actionArray=recdData.getParcelableArrayList("actionArray");
comedyArray=recdData.getParcelableArrayList("comedyArray");
loveArray=recdData.getParcelableArrayList("loveArray");
classicsArray=recdData.getParcelableArrayList("classicsArray");
familyArray=recdData.getParcelableArrayList("familyArray");
}
}
My Requirement is to Store the Bundle(recdData)
into Sharedpreferences
in Activity2?
Could any one help me?
解决方案
preferences = mActivity.getSharedPreferences(
SharePreference_demo, Context.MODE_PRIVATE);
Map<String, String> demoarraylist= new HashMap<String, String>();
demoarraylist.put("actionArray", "action");
demoarraylist.put("comedyArray", "comedy");
demoarraylist.put("lovearray", "love");
demoarraylist.put("classicArray", "classic");
demoarraylist.put("familyArray", "family");
SharedPreferences.Editor editor = preferences.edit();
for (Entry<String, String> entry : demoarraylist.entrySet()) {
editor.putString(entry.getKey(), entry.getValue());
}
editor.commit();
put above code put in your 1st activity.
and below code is put in your 2nd activity.
Map<String, String> demoarraylist= new HashMap<String, String>();
SharedPreferences preferences = activity.getSharedPreferences(
SharePreference_demo, Context.MODE_PRIVATE);
for (Entry<String, ?> entry : preferences.getAll().entrySet()) {
demoarraylist.put(entry.getKey(), entry.getValue().toString());
}
Iterator myVeryOwnIterator = demoarraylist.keySet().iterator();
while (myVeryOwnIterator.hasNext()) {
String key = (String) myVeryOwnIterator.next();
String value = (String) MapListUserStatus.get(key);
}
这篇关于保存Bundle对象到共享preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文