You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

合并不同类型ArrayList为通用集合并传递至新Activity

如何整合多类型位置数据并传递到谷歌地图Activity

嘿,我明白你现在的需求——把分散在多个ArrayList里的位置数据整合到一起,方便传到谷歌地图Activity里遍历打印对吧?其实不用纠结ArrayList<Object>,用一个自定义的数据模型类会更靠谱,类型安全还代码清爽,咱们一步步来:

1. 创建自定义数据模型类

首先定义一个LocationData类,把你需要的所有字段(名称、类型、经纬度)都封装进去,同时实现Serializable接口,这样咱们才能通过Intent把这个类的集合传递到其他Activity:

import java.io.Serializable;

public class LocationData implements Serializable {
    private String name;
    private String type;
    private double latitude;
    private double longitude;

    // 构造方法
    public LocationData(String name, String type, double latitude, double longitude) {
        this.name = name;
        this.type = type;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    // Getter方法(方便后续获取字段值)
    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    // 重写toString方法,方便直接打印数据
    @Override
    public String toString() {
        return "LocationData{" +
                "name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", latitude=" + latitude +
                ", longitude=" + longitude +
                '}';
    }
}

2. 修改数据解析方法,生成统一的集合

把你原来的getDBLocations方法改成直接生成ArrayList<LocationData>,不用再维护多个分散的ArrayList了:

private void getDBLocations(JSONArray j){
    ArrayList<LocationData> allLocations = new ArrayList<>();
    
    // 遍历JSON数组中的所有项
    for(int i=0;i<j.length();i++){
        try{
            // 获取JSON对象
            JSONObject json = j.getJSONObject(i);
            
            // 提取字段值
            String name = json.getString(Config.TAG_NAME);
            String type = json.getString(Config.TAG_TYPE);
            double longitude = json.getDouble(Config.TAG_LONGITUDE);
            double latitude = json.getDouble(Config.TAG_LATITUDE);
            
            // 创建LocationData对象并添加到集合
            allLocations.add(new LocationData(name, type, latitude, longitude));
            
        }catch (JSONException e){
            e.printStackTrace();
        }
    }
    
    // 这里就可以把allLocations传递到谷歌地图Activity了
    navigateToMapActivity(allLocations);
}

3. 通过Intent传递集合到新Activity

写一个简单的方法,把集合通过Intent传递到你的谷歌地图Activity:

private void navigateToMapActivity(ArrayList<LocationData> locations) {
    Intent intent = new Intent(this, GoogleMapActivity.class);
    // 把集合作为Extra传递,因为LocationData实现了Serializable
    intent.putExtra("ALL_LOCATIONS", locations);
    startActivity(intent);
}

4. 在谷歌地图Activity中接收并遍历打印

在你的谷歌地图Activity的onCreate方法里,接收传递过来的集合,然后就可以轻松遍历打印了:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_google_map);
    
    // 获取传递过来的集合
    ArrayList<LocationData> locations = (ArrayList<LocationData>) getIntent().getSerializableExtra("ALL_LOCATIONS");
    
    // 遍历并打印所有数据
    if (locations != null && !locations.isEmpty()) {
        for (LocationData location : locations) {
            // 直接用toString打印,或者逐个获取字段打印
            Log.d("LocationInfo", location.toString());
            
            // 也可以单独获取字段:
            // Log.d("LocationName", location.getName());
            // Log.d("LocationType", location.getType());
        }
    }
}

为什么不推荐用ArrayList<Object>?

用ArrayList<Object>虽然能装各种类型的数据,但后续遍历的时候需要频繁做类型强转,不仅代码繁琐,还容易出现ClassCastException,类型完全不安全。而自定义模型类的方式更符合面向对象的设计,代码可读性和可维护性都高得多。

如果你的数据量特别大,追求更高的传递性能,可以把Serializable换成Parcelable接口(Android官方推荐的高效序列化方式),不过日常使用Serializable已经足够简单好用啦。

内容的提问来源于stack exchange,提问作者Ioan Kats

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:19:34