进入数组的下一个索引

时间:2017-02-05 12:12:39

标签: android arrays

我试图递增数组的下一个索引,但它只递增一个索引,然后它将转到0并且它不会转到下一个索引。 以下是示例:http://imgur.com/a/AkHGn

以下是变量:

  ArrayList<JSONArray> listOfRouteArray = new ArrayList<>();
    private ArrayList<Integer> listOfIndicesOfCurrentRoutes = new ArrayList<>();
    private ArrayList<Polyline> polylines = new ArrayList<>();

以下是我的功能:

  public void alternateRoute(){

        // If the displayed polyline of the route to the first destination is not the last alternative
        // polyline, display the next alternative polyline else called the
        // incrementRouteIndexOfNextOrderPolyline() function. Take a look at
        // incrementRouteIndexOfNextOrderPolyline() and look at this example. example: if we have
        // 138 the next count is 139.


        if(!listOfRouteArray.isEmpty()){
            if((listOfIndicesOfCurrentRoutes.get(0) + 1) <
                    listOfRouteArray.get(0).length()){

                alternatePolyline(0, listOfIndicesOfCurrentRoutes.get(0) + 1);

            }
            else {
                incrementRouteIndexOfNextOrderPolyline(0);
            }
        }

    }

递增索引:

 private void incrementRouteIndexOfNextOrderPolyline(int indexOfPolyline){

        //sets the polyline of the route to a destination to the first polyline of the list of all
        //possible polylines of the route to that destination. Tha is if a digit was 9 set it to 0.
        alternatePolyline(indexOfPolyline,0);


        //if the route to destination with dealing with is the route to the lastly added destination
        //return. that is if the digit is 9 in 9999 return.
        if((indexOfPolyline + 1) >= polylines.size()){

        return;
        }

        //if the polyline of the next order route to destination is not that last possible alternative
        //polyline for that route, set it to the next possible polyline else increment the index of it's
        // next order polyline. that is if we have 118 and we are dealing with 8 make the next count to
        // be 119 else we have 119 make it to be 120
        if((listOfIndicesOfCurrentRoutes.get(indexOfPolyline + 1) + 1) <
                listOfRouteArray.get(indexOfPolyline + 1).length()){

            alternatePolyline(indexOfPolyline,
                    listOfIndicesOfCurrentRoutes.get(indexOfPolyline + 1) + 1);


        }else {
            incrementRouteIndexOfNextOrderPolyline(indexOfPolyline + 1);

        }

这可能需要澄清:

 public void alternatePolyline(int indexOfRouteToAlternate, int indexOfNewPolyline){

        listOfIndicesOfCurrentRoutes.set(indexOfRouteToAlternate, indexOfNewPolyline);

        try {
            JSONObject routes = listOfRouteArray.get(indexOfRouteToAlternate).getJSONObject(indexOfNewPolyline);
            JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
            String encodedString = overviewPolylines.getString("points");
            List<LatLng> list = decodePoly(encodedString);
            PolylineOptions options = new PolylineOptions().width(10).color(Color.GREEN).geodesic(true);
            for (int z = 0; z < list.size(); z++) {
                LatLng point = list.get(z);
                options.add(point);
            }


            polylines.get(indexOfRouteToAlternate).remove();
            polylines.set(indexOfRouteToAlternate, mMap.addPolyline(options));

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

我已经在mediafire中上传了整个项目,我个人可以提供源代码来解决这个问题。

0 个答案:

没有答案