0

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

我的要求是将 in 存储Bundle(recdData)SharedpreferencesActivity2 中?任何人都可以帮助我吗?

4

3 回答 3

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);
}
于 2013-09-16T06:40:38.423 回答
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 类

于 2013-09-16T06:22:46.627 回答
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());
            }

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

于 2013-09-16T06:26:02.940 回答