替换Array List Android中特定索引的数据

时间:2014-09-10 04:40:34

标签: android arraylist google-maps-markers google-maps-api-2

我正在尝试用新的数据替换数组列表数据,但它总是给我错误的数据。第一次正确替换,但下次在循环中它替换了Array List的所有索引。这是我的代码和日志猫,我在其中存储地图标记ID

    //Declaring Arraylists
ArrayList<ArrayList<LatLng>> LatLng_List_for_editing = new ArrayList<ArrayList<LatLng>>();
private ArrayList<ArrayList<String>> ID_List_for_editing = new ArrayList<ArrayList<String>>();
private ArrayList<String> ID_List = new ArrayList<String>();
private ArrayList<LatLng> LatLng_List = new ArrayList<LatLng>();

//inserting data

ID_List_for_editing.add(new ArrayList<String>(ID_List));
LatLng_List_for_editing.add(new ArrayList<LatLng>(LatLng_List));

//getting data
for (int p_index = 0; p_index < LatLng_List_for_editing.size(); p_index++) {
                    for (int c_index = 0; c_index < LatLng_List_for_editing.get(p_index).size(); c_index++) {

                        if (ID_List_for_editing.get(p_index).get(c_index).equals(maker_ID_being_draged)) {
                            LatLng_List_for_editing.get(p_index).remove(c_index);
                            LatLng_List_for_editing.get(p_index).add(c_index, ID_latlng);
                            Log.e("Lat lng list during editing :", "" + LatLng_List_for_editing.get(p_index));
                        }
                    }
                    ID_List.clear();
                    MarkerOptions marker = new MarkerOptions();
                    PolylineOptions polylineOptions = new PolylineOptions();
                    polylineOptions.color(Color.RED);
                    polylineOptions.width(3);
                    polylineOptions.addAll(LatLng_List_for_editing.get(p_index));
                    mMap.addPolyline(polylineOptions);
                    for (int lenghth = 0; lenghth < LatLng_List_for_editing.get(p_index).size(); lenghth++) {
                        Marker_ID = mMap.addMarker(
                                marker.position(LatLng_List_for_editing.get(p_index).get(lenghth)).draggable(
                                        true)).getId();
                            ID_List.add(Marker_ID);
                        }
                    Log.e("ID's in edit list is :", "" + ID_List_for_editing);
                    Log.e("ID list before removing :", ":"+p_index + ID_List_for_editing.get(p_index));
                    ID_List_for_editing.remove(p_index);
                        ID_List_for_editing.add(p_index ,ID_List);
                    Log.e("ID list is :", "" + ID_List);
                    Log.e("ID list after removing :", ":"+p_index + ID_List_for_editing.get(p_index));
                    Log.e("IDs in edit list is :", "" + ID_List_for_editing);
                }
                Log.e("final Lat lng list:", "" + LatLng_List_for_editing);

这是在替换之前和之后显示数组数据的日志cat。

    09-10 09:25:14.931: E/Start draging Marker(19149): m5
09-10 09:25:16.431: E/new Marker ID coordinates id :(19149): lat/lng: (33.673322993023035,73.07959388941526)
09-10 09:25:16.441: E/ID's in edit list is :(19149): [[m0, m1, m2], [m3, m4, m5]]
09-10 09:25:16.441: E/ID list before removing :(19149): :0[m0, m1, m2]
09-10 09:25:16.441: E/ID list is :(19149): [m6, m7, m8]
09-10 09:25:16.441: E/ID list after removing :(19149): :0[m6, m7, m8]
09-10 09:25:16.441: E/IDs in edit list is :(19149): [[m6, m7, m8], [m3, m4, m5]]
09-10 09:25:16.446: E/Lat lng list during editing :(19149): [lat/lng: (33.672664222061485,73.0725671723485), lat/lng: (33.67516423249219,73.07723086327314), lat/lng: (33.673322993023035,73.07959388941526)]
09-10 09:25:16.451: E/ID's in edit list is :(19149): [[m9, m10, m11], [m3, m4, m5]]
09-10 09:25:16.451: E/ID list before removing :(19149): :1[m3, m4, m5]
09-10 09:25:16.451: E/ID list is :(19149): [m9, m10, m11]
09-10 09:25:16.451: E/ID list after removing :(19149): :1[m9, m10, m11]
09-10 09:25:16.456: E/IDs in edit list is :(19149): [[m9, m10, m11], [m9, m10, m11]]
09-10 09:25:16.456: E/final Lat lng list:(19149): [[lat/lng: (33.66989794278846,73.07382009923458), lat/lng: (33.6673977792688,73.07181749492884), lat/lng: (33.66856443827623,73.0777683109045)], [lat/lng: (33.672664222061485,73.0725671723485), lat/lng: (33.67516423249219,73.07723086327314), lat/lng: (33.673322993023035,73.07959388941526)]]

请帮忙。感谢

1 个答案:

答案 0 :(得分:2)

尝试列表界面并使用以下方法设置数据

set(int index, E element)

使用指定的对象替换此List中指定位置的元素。此操作不会更改列表的大小。

<强>参数

location:放置指定对象的索引。

object:要插入的对象。

<强>返回     索引的前一个元素。