如何将ArrayList<HashMap<String, String>>从Activity传递到Fragment?
Hey there! Let's break down exactly how to pass your ArrayList<HashMap<String, String>> from an Activity to a Fragment—this is a super common task, and I’ve got two reliable approaches for you that follow Android best practices.
Method 1: Use setArguments() (Recommended for Simple Passing)
This is the go-to method because it ties your data directly to the Fragment's instance state, so it won't get lost during configuration changes (like screen rotations). Since both ArrayList and HashMap<String, String> implement the Serializable interface, we can safely pass them via a Bundle.
Step 1: Pass the data from your Activity
// In your Activity // First, create and populate your list ArrayList<HashMap<String, String>> myDataList = new ArrayList<>(); // Add your HashMap entries to the list here... // Create your Fragment instance YourTargetFragment fragment = new YourTargetFragment(); // Create a Bundle and insert your list Bundle args = new Bundle(); args.putSerializable("KEY_DATA_LIST", myDataList); // Attach the Bundle to the Fragment fragment.setArguments(args); // Add/replace the Fragment using FragmentManager getSupportFragmentManager().beginTransaction() .replace(R.id.your_fragment_container, fragment) .commit();
Step 2: Retrieve the data in your Fragment
// In your Fragment @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { // Suppress unchecked cast warning (safe here since we know the type) @SuppressWarnings("unchecked") ArrayList<HashMap<String, String>> receivedList = (ArrayList<HashMap<String, String>>) getArguments().getSerializable("KEY_DATA_LIST"); // Now you can use receivedList in your Fragment! } }
Method 2: Share Data with a ViewModel (Great for Complex Scenarios)
If you need to share data between multiple components (like multiple Fragments and an Activity) or want to retain data across configuration changes more seamlessly, a ViewModel is perfect.
Step 1: Create a shared ViewModel
public class SharedDataViewModel extends ViewModel { private final MutableLiveData<ArrayList<HashMap<String, String>>> dataList = new MutableLiveData<>(); // Set the data from the Activity public void setDataList(ArrayList<HashMap<String, String>> list) { dataList.setValue(list); } // Get the data in the Fragment public LiveData<ArrayList<HashMap<String, String>>> getDataList() { return dataList; } }
Step 2: Set the data in your Activity
// In your Activity SharedDataViewModel viewModel = new ViewModelProvider(this).get(SharedDataViewModel.class); viewModel.setDataList(myDataList); // Launch your Fragment as usual YourTargetFragment fragment = new YourTargetFragment(); getSupportFragmentManager().beginTransaction() .replace(R.id.your_fragment_container, fragment) .commit();
Step 3: Observe the data in your Fragment
// In your Fragment's onViewCreated method @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); SharedDataViewModel viewModel = new ViewModelProvider(requireActivity()).get(SharedDataViewModel.class); viewModel.getDataList().observe(getViewLifecycleOwner(), list -> { // Update your UI or use the data here if (list != null) { // Do something with your ArrayList<HashMap<String, String>> } }); }
Quick Pro Tip
Avoid passing data directly through the Fragment's constructor! When the system recreates your Fragment (e.g., after a screen rotation), the constructor will be called again, and your data will be lost. Stick to setArguments() or ViewModel for safe, reliable data passing.
内容的提问来源于stack exchange,提问作者Trusted

