问题
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?
回答1:
in your Activity2
public void addTask(Task t) {
if (null == currentTasks) {
currentTasks = new ArrayList<task>();
}
currentTasks.add(t);
//save the task list to preference
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Editor editor = prefs.edit();
try {
editor.putString(TASKS, ObjectSerializer.serialize(currentTasks));
} catch (IOException e) {
e.printStackTrace();
}
editor.commit();
}
Similarly we have to retrieve the list of tasks from the preference in the onCreate() method:
public void onCreate() {
super.onCreate();
if (null == currentTasks) {
currentTasks = new ArrayList<task>();
}
// load tasks from preference
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
try {
currentTasks = (ArrayList<task>) ObjectSerializer.deserialize(prefs.getString(TASKS, ObjectSerializer.serialize(new ArrayList<task>())));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
You can get ObjectSerializer class from Apache Pig project ObjectSerializer.java
回答2:
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);
}
回答3:
Map<String, String> MapListUserStatus = new HashMap<String, String>();
SharedPreferences.Editor editor = preferences.edit();
for (Entry<String, String> entry : MapListUserStatus.entrySet()) {
editor.putString(entry.getKey(), entry.getValue());
}
Store your ArrayListData into sharedprefernces using HashMap().
Retrieve sharedPreferences data:
MapListUserStatus = new HashMap<String, String>();
SharedPreferences preferences = conductpd.getSharedPreferences(
SharePreference_messages_history, Context.MODE_PRIVATE);
for (Entry<String, ?> entry : preferences.getAll().entrySet()) {
MapListUserStatus.put(entry.getKey(), entry.getValue().toString());
}
I hope its useful to you..if any query.please tell me.
来源:https://stackoverflow.com/questions/18821405/saving-bundle-object-into-shared-preference