嗨,我有这个ArrayList

public static List<JSONObject> markerList = new ArrayList<JSONObject>();


我用for(循环)在其中放了一个JSONObject
来自服务器的ID以及一些信息
和标记ID(Google地图..)

如何检查数组中的此对象是否具有特定的ID,所以不再插入
因为此对象将具有不同的值(例如纬度和经度以及标记ID),但对于ArrayList中所有重复的对象将具有相同的驱动程序ID,所以我真的在想如何检查它?

public static List<JSONObject> markerList = new ArrayList<JSONObject>();
public static void CreateMarker(JSONArray arrayList){
    try {
        Marker marker;
        for (int counter = 0; counter < arrayList.length(); counter++) {
            JSONObject json=arrayList.getJSONObject(counter);
            int id = json.getInt("id");
            int is_active = json.getInt("is_active");
            double latitude = json.getDouble("latitude");
            double longitude = json.getDouble("longitude");

                MarkerOptions markerOptions = new MarkerOptions()
                        .position(new LatLng(latitude, longitude))
                        .title(String.valueOf(id))
                        .flat(true)
                        .anchor(0.5f, 1.0f)
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.car_marker_dark));
                marker = mGoogleMap.addMarker(markerOptions);
                Log.e("MarkerList", "Marker id '" + marker.getId() + "' added to list.");
                Log.e("MarkerList", String.valueOf(markerList));
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id",id);
                jsonObject.put("is_active",is_active);
                jsonObject.put("latitude",latitude);
                jsonObject.put("longitude",longitude);
                jsonObject.put("Marker",marker);
                markerList.add(jsonObject);

        }
    }catch (Exception e){
        e.printStackTrace();
    }
}

最佳答案

解决这个问题的可能方法是使用HashMap代替ArrayList,其键为id,值为JSONObject。这样,您可以在插入HashMap之前检查它是否已经包含您的密钥。

07-28 02:01
查看更多