在两个活动之间传递数组列表值可以通过以下几种方式实现:
示例代码: 发送活动:
ArrayList<String> myList = new ArrayList<>();
// 添加数据到myList
Intent intent = new Intent(this, ReceiverActivity.class);
intent.putExtra("myList", myList);
startActivity(intent);
接收活动:
Intent intent = getIntent();
ArrayList<String> receivedList = (ArrayList<String>) intent.getSerializableExtra("myList");
示例代码:
public class DataHolder {
public static ArrayList<String> myList = new ArrayList<>();
}
// 在发送活动中
DataHolder.myList = myList;
// 在接收活动中
ArrayList<String> receivedList = DataHolder.myList;
示例代码: 发送活动:
ArrayList<String> myList = new ArrayList<>();
// 添加数据到myList
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet("myList", new HashSet<>(myList));
editor.apply();
接收活动:
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
Set<String> receivedSet = sharedPreferences.getStringSet("myList", new HashSet<>());
ArrayList<String> receivedList = new ArrayList<>(receivedSet);
这些方法可以根据具体需求选择合适的方式来在两个活动之间传递数组列表值。
领取专属 10元无门槛券
手把手带您无忧上云