for循环中的类

时间:2011-03-23 01:30:50

标签: android class loops for-loop

新手,我上课了:

类PolyOverlay扩展了Overlay {

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow)
    {

        try {

        List<GeoPoint> points = new ArrayList<GeoPoint>();
        GeoPoint tmpPoint;

        double lat2;
        double lng2;

        String args[] = {"",""};
        args = sitesList.getpolyLine().get(daI).split("\\;");
        //Toast.makeText(getApplicationContext(),"T: " + args[0],Toast.LENGTH_SHORT ).show();


        for (int i = 0; i < args.length; i++) {
            String coordinates2[] = {"",""};
            String args2[] = {"",""};
            args2 = args[i].split("\\,");

            coordinates2[0]= args2[0];
            coordinates2[1]= args2[1];

          //Toast.makeText(getApplicationContext(),"T: " + coordinates2[0],Toast.LENGTH_SHORT ).show();


            lat2= Double.parseDouble(coordinates2[0]);
            lng2= Double.parseDouble(coordinates2[1]);
            tmpPoint = new GeoPoint((int) (lat2 * 1E6), (int) (lng2 * 1E6) );                   
            points.add(tmpPoint);


        }


        //for(Polygon polygonTemp : polygonList)
        //{           
            Path p = new Path();
            Projection projection = mapView.getProjection();

            boolean firstTime = true;
            for(GeoPoint geoPoint : points)
            {
                Point drawPoint = new Point();
                projection.toPixels(geoPoint, drawPoint);

                if(firstTime)
                {
                    p.moveTo(drawPoint.x, drawPoint.y);
                    firstTime = false;
                }
                else
                {
                    p.lineTo(drawPoint.x, drawPoint.y);
                }
            }
            p.setFillType(Path.FillType.EVEN_ODD);

            Paint polyPaint = new Paint();
            polyPaint.setStrokeWidth(1);
            polyPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            polyPaint.setAntiAlias(true);
            polyPaint.setColor(Color.BLUE);
            polyPaint.setAlpha(30);
            canvas.drawPath(p, polyPaint);
            firstTime = true;
        //}
        super.draw(canvas, mapView, shadow);
        } catch (Exception e) {
            Toast.makeText(epapp.this, "Err: " + e, Toast.LENGTH_LONG).show();

        }
    }

}

然后在xml循环中我这样做:

for (int i = 0; i < sitesList.getStrap().size(); i++) {

            //icons

            String pargs[] = sitesList.getCentr().get(i).split("\\,");

            String coordinates3[] = {"",""};
            coordinates3[0] = pargs[0].toString();
            coordinates3[1] = pargs[1].toString();

            //Toast.makeText(epapp.this, "Center: " + coordinates3[0], Toast.LENGTH_LONG).show();

            double lat2 = Double.parseDouble(coordinates3[0]);
            double lng2 = Double.parseDouble(coordinates3[1]);



            np = new GeoPoint(
                (int) (lat2 * 1E6), 
                (int) (lng2 * 1E6)); 
            Drawable drawable = this.getResources().getDrawable(R.drawable.iconresidential);
            IconItemizedOverlay itemizedoverlay = new IconItemizedOverlay(drawable,this);


            OverlayItem overlayitem = new OverlayItem(np, "Property Information", "JONES EMERSON ROBERT JR & SARA MARIA<br>1032 Briarwood Dr Wauchula 33873<br>HARDEE County<br>STRAP 05-34-25-0865-00001-0020");
            itemizedoverlay.addOverlay(overlayitem);
            mapView.getOverlays().add(itemizedoverlay);


            //if (i==1){


        //polyString = sitesList.getpolyLine().get(i);
        daI=i;  
        //polygon

            PolyOverlay myPolyOverlay = new PolyOverlay();
            mapView.getOverlays().add(myPolyOverlay);

                   //}  

        }

好的所以“问题”是如果我在//“if(i == 1){{循环中”将“daI”设置为“i”的部分,我的“Polygon”会显示在地图上XML中的那个项目,如果我“REM”它,多边形覆盖始终是XML中的最后一项。 xml列表总是列表中有20个项目,好像“daI”变量总是19(列表0-19中的最后一个索引)

显然没有做某事,所以PolyOverlay类在xml循环完成之前得到“完成”?就像xml循环通过设置“daI”飞到最后一个,PolyOverlay正在为最后一个项加载多边形...

我的图标在地图上就好了......

1 个答案:

答案 0 :(得分:2)

k,我很迟钝:我必须这样做:

   int di = 0;

    PolyOverlay(int n) {
        di = n;
    }

在我的PolyOvelay类中然后将“i”从xml for循环传递给它......

PolyOverlay myPolyOverlay = new PolyOverlay(i);

调用MapView.getOverlays()添加(myPolyOverlay);