使用Intent传递数据时for语句仅返回最后一条数据的问题求助
Hey there! You’re spot-on to suspect the for loop here—this is a super common issue when passing collections of data via Intents. Let’s break down what’s probably going wrong and how to fix it.
What’s Causing the Problem?
Chances are, in your getAllDataLocation() method, you’re doing something like this in the for loop:
Intent intent = new Intent(this, TargetActivity.class); for (YourLocationItem item : yourLocationList) { // Overwriting the same key every time intent.putExtra("LOCATION_DATA", item); } startActivity(intent);
Every time you call putExtra() with the same key, you’re replacing the previous value. By the end of the loop, only the last item remains in the Intent’s extras.
How to Fix It
The solution is to pass the entire ArrayList of data at once, instead of overwriting the same key repeatedly. First, make sure your custom data class implements Parcelable (recommended for efficiency) or Serializable.
Step 1: Make Your Data Class Parcelable
Let’s say your data class is LocationModel—here’s how to implement Parcelable for it:
public class LocationModel implements Parcelable { private String locationName; private double latitude; private double longitude; // Constructor public LocationModel(String locationName, double latitude, double longitude) { this.locationName = locationName; this.latitude = latitude; this.longitude = longitude; } // Parcelable required methods protected LocationModel(Parcel in) { locationName = in.readString(); latitude = in.readDouble(); longitude = in.readDouble(); } public static final Creator<LocationModel> CREATOR = new Creator<LocationModel>() { @Override public LocationModel createFromParcel(Parcel in) { return new LocationModel(in); } @Override public LocationModel[] newArray(int size) { return new LocationModel[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(locationName); dest.writeDouble(latitude); dest.writeDouble(longitude); } // Getters (add setters if needed) public String getLocationName() { return locationName; } public double getLatitude() { return latitude; } public double getLongitude() { return longitude; } }
Step 2: Adjust Your getAllDataLocation() Method
Instead of looping to add individual items, pass the entire ArrayList to the Intent:
private void getAllDataLocation() { loading = ProgressDialog.show(this, null, "Menampilkan Data Lokasi..."); // Assume this is where you fetch/populate your list of locations ArrayList<LocationModel> locationList = new ArrayList<>(); // Add your location items to the list here... // Pass the entire list via Intent Intent intent = new Intent(this, YourTargetActivity.class); intent.putParcelableArrayListExtra("ALL_LOCATIONS", locationList); startActivity(intent); loading.dismiss(); }
Step 3: Receive the ArrayList in the Target Activity
In your destination Activity, retrieve the full list like this:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_target_layout); // Get the full list of locations ArrayList<LocationModel> receivedLocations = getIntent().getParcelableArrayListExtra("ALL_LOCATIONS"); // Now you can use the entire list (e.g., populate a RecyclerView/ListView) if (receivedLocations != null && !receivedLocations.isEmpty()) { // Your logic to display or process all items } }
Why This Works
By using putParcelableArrayListExtra(), you’re sending the entire collection as a single extra. No more overwriting values—all your location items will be preserved and accessible in the target Activity.
内容的提问来源于stack exchange,提问作者Kusnadi

