将Bundle对象保存为共享首选项

时间:2013-09-16 06:11:39

标签: android android-intent android-emulator bundle

我有2项活动

活性1

活性2

活性1:

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); 

活性2:

活性2:

 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");
        }
    }

我的要求是将Bundle(recdData)存储到Activity {2中的Sharedpreferences? 有谁可以帮助我?

3 个答案:

答案 0 :(得分:1)

在你的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();
}

同样,我们必须在onCreate()方法中从首选项中检索任务列表:

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();
    }
}

您可以从Apache Pig项目ObjectSerializer.java

获取ObjectSerializer类

答案 1 :(得分:1)

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();

将上面的代码放在第一个活动中。

以下代码放在第二个活动中。

 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);
}

答案 2 :(得分:0)

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());
                }

使用HashMap()将ArrayListData存储到sharedprefernces中。

检索sharedPreferences数据:

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());
            }

我希望它对你有用..如果有任何疑问,请告诉我。

相关问题