Android:为什么我不能在我的arraylist中保存更多的对象

时间:2013-01-26 00:39:24

标签: android arraylist

为什么当我触摸精确定位时,它只需要arraylist中的最后一个精确定位点。当我测量arraylist大小时,它表示即使我保存2个或更多精确定位,大小也是1。

GoogleMaps Class。

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();
    }

}

1 个答案:

答案 0 :(得分:0)

您在getData()中重复向列表中添加相同的Locations对象...您没有实例化新对象。

你可能真的意味着

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;