0

タッチされたピンポイントを作成すると、配列リストの最後のピンポイントのみが取得されるのはなぜですか。arraylist のサイズを測定すると、2 つ以上のピンポイントを保存しているにもかかわらず、サイズが 1 と表示されます。

グーグルマップクラス。

public class GoogleMaps extends MapActivity implements LocationListener {



public void addLocation() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    final EditText input = new EditText(this);
    alert.setTitle("What do you want to call the location?");
    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            value = input.getText().toString().trim();
            checklocationTitle();
        }
    });

    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                }
            });
    alert.show();
}


public void checklocationTitle() {
    if (value.length() > 3) {
        Toast.makeText(this, "Name of the locations is know " + value,
                Toast.LENGTH_LONG).show();
        try {
            markedpinpoint = true;
            midllat = touchedPoint.getLatitudeE6() / 1E6;
            midlongi = touchedPoint.getLongitudeE6() / 1E6;
            Geocoder geocoder = new Geocoder(getBaseContext(),
                    Locale.getDefault());
            List<Address> adress = geocoder.getFromLocation(
                    touchedPoint.getLatitudeE6() / 1E6,
                    touchedPoint.getLongitudeE6() / 1E6, 1);
            if (adress.size() > 0) {
                String display = "";
                for (int i = 0; i < adress.get(0).getMaxAddressLineIndex(); i++) {
                    display += adress.get(0).getAddressLine(i) + "\n";
                    OverlayItem overlayitem = new OverlayItem(touchedPoint,
                            value, display);
                    custom = new Location_Service(d, GoogleMaps.this);
                    custom.insertLocation(overlayitem);
                    overlayList.add(custom);
                }
            } else {
                Toast.makeText(
                        this,
                        "There where a problem to locate the selected adresse",
                        Toast.LENGTH_LONG).show();
            }
        } catch (IOException e) {
        }
    } else {
        Toast.makeText(this,
                "Please provide a least 3 cifre Title for your location.",
                Toast.LENGTH_LONG).show();
        addLocation();
    }
}


public void buttonLocations(View view) {
    // stopLocationListner();
    // stopBackgroundService();
    Intent intent = new Intent(this, PinPoints.class);
    startActivity(intent);
    // Toast.makeText(this, "Gemte steder: " + custom.size(),
    // Toast.LENGTH_LONG).show();

}

}

Location_Service クラス

public class Location_Service extends ItemizedOverlay<OverlayItem> {

    public ArrayList<OverlayItem> pinpoints = new ArrayList<OverlayItem>();

    public Location_Service(Drawable defaultMarker) {
        super(boundCenter(defaultMarker));
        // TODO Auto-generated constructor stub
    }

    public ArrayList<Locations> getData() {
        Locations hej = new Locations();
        ArrayList<Locations> tt = new ArrayList<Locations>();
        for (OverlayItem test : pinpoints) {
            hej.setAdress(test.getSnippet());
            hej.setMidlat(test.getPoint().getLatitudeE6());
            hej.setMidlong(test.getPoint().getLongitudeE6());
            hej.setTitle(test.getTitle());
            tt.add(hej);
        }
        return tt;
    }

    public Location_Service(Drawable m, Context context) {
        this(m);
    }

    @Override
    protected OverlayItem createItem(int i) {
        return pinpoints.get(i);
    }

    @Override
    public int size() {
        return pinpoints.size();
    }

    public void insertLocation(OverlayItem item) {
        pinpoints.add(item);
        this.populate();
    }

}
4

1 に答える 1

0

同じ Locations オブジェクトをリストに繰り返し追加してgetData()います...新しいオブジェクトをインスタンス化していません。

あなたはおそらく本当に意味した

ArrayList<Locations> tt = new ArrayList<Locations>();
for (OverlayItem test : pinpoints) {
    Locations hej = new Locations(); //instantiate each time!
    hej.setAdress(test.getSnippet());
    hej.setMidlat(test.getPoint().getLatitudeE6());
    hej.setMidlong(test.getPoint().getLongitudeE6());
    hej.setTitle(test.getTitle());
    tt.add(hej);
}
return tt;
于 2013-01-26T00:59:55.963 に答える